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!
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.
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.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With