使用Google-Maps-for-Rails Gem我如何找到指定距离内的点到指定点? [英] Using the Google-Maps-for-Rails Gem how would I find points within a specified distance to a given point?
问题描述
我想知道是否可以使用Google-Maps-for-Rails gem来查找保存在数据库中的点的列表,这些点位于给定点的指定半径范围内。例如,我如何找到距离自由女神像20英里内的数据库中的所有点。
我没有发现任何可以解决这种情况的文档,除非我错过了它。
感谢您的期待。
我认为Google Maps for Rails不会开箱即用。我最终使用Google Maps for Rails来生成地图,并使用Geocoder gem来进行地理编码和确定数据点的范围。
有了这两个宝石,在我的模型中我有:
class存储< ActiveRecord :: Base
acts_as_gmappable
private
def gmaps4rails_address
#{address_line_1},#{self.city},#{self.region },#{self.postal_code},#{self.country}
end
end
在我的控制器中,我有:
def index
if params [:search]
@stores = Store.near(params [:search],20)
@json = @ stores.to_gmaps4rails
else
@stores = Store.all
@json = @ stores.to_gmaps4rails
结束
respond_to do | format |
format.html#index.html.erb
format.json {render json:@stores}
end
end
这似乎工作得很好,但如果有更好的结果,我会留下这个问题回答。
I'm wondering if its possible to use the Google-Maps-for-Rails gem to find a list of points that are saved in the database that are within a specified radius of a given point. For example, how would I find all the points in my database that are within 20 miles of the Statue of Liberty.
I couldn't find anything in the documentation that goes over this scenario, unless I missed it.
Thank you for looking.
I don't think Google Maps for Rails does this out of the box. I ended up using Google Maps for Rails for taking care of generating the map, and using the Geocoder gem to do geocoding and scoping my data points.
With both gems, in my model I have:
class Store < ActiveRecord::Base
acts_as_gmappable
private
def gmaps4rails_address
"#{address_line_1}, #{self.city}, #{self.region}, #{self.postal_code}, #{self.country}"
end
end
In my controller I have:
def index
if params[:search]
@stores = Store.near(params[:search], 20)
@json = @stores.to_gmaps4rails
else
@stores = Store.all
@json = @stores.to_gmaps4rails
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @stores }
end
end
This seems to be working just fine, but I'll leave this question open a bit in case there's a better answer.
这篇关于使用Google-Maps-for-Rails Gem我如何找到指定距离内的点到指定点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!