Rails, Geocoder and nearby

I have a little problem trying to display specific locations based on user proximity. In my controller, I have the following:

if User.find(current_user) @user = User.find(current_user) @locations = Location.near(params[:latitude => @user.latitude, :longitude => @user.longitude], 50, :order => :distance) end 

Users have latitude and longitude. I think that I do not have the correct parameters in the Location.near line, but I cannot understand what they should be.

Any help would be appreciated.

Hurrah!

+6
source share
2 answers

Let's try to overwrite that bit, current_user should already be set, no need to call User.find. Then it looks like you can pass lat as long as an array

  @locations = Location.near([current_user.latitude, current_user.longitude], 50, :order => :distance) 

http://railscasts.com/episodes/273-geocoder

http://www.rubygeocoder.com/

https://github.com/alexreisner/geocoder (check readme)

+6
source

You can determine the distance you want to find, remember also transfer the device:

 @locations = Location.near([current_user.latitude, current_user.longitude], 50, units: :km) 
+2
source

Source: https://habr.com/ru/post/920386/


All Articles