我想知道是否可以使用Google-Maps-for-Rails gem来查找保存在数据库中、位于给定点指定半径内的点的列表。例如,我如何在我的数据库中找到自由女神像20英里范围内的所有点。
我在这个场景的文档中找不到任何东西,除非我遗漏了它。
感谢您的关注。
发布于 2011-11-15 23:49:07
我认为Google Maps for Rails不会开箱即用。我最终使用Google Maps for Rails来负责生成地图,并使用地理编码器gem来对我的数据点进行地理编码和范围划分。
对于这两个gem,在我的模型中,我有:
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在我的控制器中,我有:
定义索引
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结束
这似乎工作得很好,但我还是把这个问题留下来,以防有更好的答案。
https://stackoverflow.com/questions/8138054
复制相似问题