Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 999
Chris Valentine Avatar asked Mar 25 '23 06:03

Chris Valentine


1 Answers

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.

like image 74
thank_you Avatar answered Apr 06 '23 08:04

thank_you