Get nearby request.location with ruby ​​/ rail coverage geocoder gem

How would you get locations near your current location with a geocoding gem for rubies on 3.2.x rails?

I know that to search for locations near one of your other places you will use

@myClass.nearbys(50) 

but I would like them to be next to the location of the browsers (request.location), and not next to my other location.

To make it look like

 request.location.nearby(50) 

or something like that.

How can this be achieved?

thanks

+4
source share
1 answer

If you have the longitude and latitude of the user's location, you can make such a request

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

It was also expected that your table, i.e. in the above Location example, must be lat and lon .

Is this what you were looking for?

EDIT: I'm not sure if request.location will work in the model. Otherwise, you can always set it as a parameter to a model method inside your controller so that your logic remains divided. But if possible, save request.location in the model.

+7
source

All Articles