Using the Google-Maps-for-Rails Gem how would I fi

2019-05-29 21:45发布

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.

1条回答
Melony?
2楼-- · 2019-05-29 21:45

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.

查看更多
登录 后发表回答