Chris Valentine
Chris Valentine

Reputation: 1637

Get locations nearby request.location with ruby/rails geocoder gem

How would you get locations that are nearby your current location with geocoder gem for ruby on rails 3.2.x?

I know that to find locations near one of your other locations you would use

@myClass.nearbys(50)

but I would like them nearby the browsers location (request.location) instead of nearby another one of my locations.

Such that it would be similar to

request.location.nearby(50)

or something to that effect.

How can this be achieved?

Thanks

Upvotes: 4

Views: 6597

Answers (1)

thank_you
thank_you

Reputation: 11107

If you have the longitude and latitude of the user's location, you can make a query like so

location_info = request.location

@locations = Location.near([location_info.latitude, location_info.longitude], radius_distance_you_want_to_include)

It would also be expected that your table, i.e. in the above example Location there should be lat and lon fields.

Is this something you were looking for?

EDIT: I'm not sure if request.location will work in the model. In case it doesn't, you can always set it as a parameter into a model method inside your controller so that your logic stays separated. But if you can, keep request.location in the model.

Upvotes: 7

Related Questions