Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, Geocoder, and near

I'm having a slight issue trying to display certain locations based on proximity to a user. In my controller I have this:

  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 a latitude and longitude stored. I'm thinking I've not got the right parameters in the Location.near line, but I can't figure out what they should be.

Any help would be appreciated.

Cheers!

like image 707
t56k Avatar asked Jul 13 '12 04:07

t56k


People also ask

What is Geocoder in Rails?

Geocoder is a complete geocoding solution for Ruby. With Rails it adds geocoding (by street or IP address), reverse geocoding (finding street address based on given coordinates), and distance queries. It's as simple as calling geocode on your objects, and then using a scope like Venue.

What is geocoding in Android Studio?

Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address.


2 Answers

Lets try re-writing that a bit, current_user should already be set, no need for calling User.find. Then it looks like you can pass lat,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 the readme)

like image 52
house9 Avatar answered Oct 12 '22 03:10

house9


You can define the distance that you want search, remember pass the unit, too:

@locations = Location.near([current_user.latitude, current_user.longitude], 50, units: :km)
like image 37
monteirobrena Avatar answered Oct 12 '22 02:10

monteirobrena