0

I have latitude and longitude of all the user. I have to find the user who is near to me (around 10 miles) from my current latitude and longitude. And i have to give it as response in json. I have tried to configure geokit gem for this but it showed me some error like

WARNING: geokit-rails requires the Geokit gem. You either don't have the gem installed,
or you haven't told Rails to require it. If you're using a recent version of Rails: 
  config.gem "geokit" # in config/environment.rb
and of course install the gem: sudo gem install geokit

so i have moved to geocoder. What should i do with this geocoder for finding users with the above said conditions? please help me.

logesh
  • 2,572
  • 4
  • 33
  • 60

1 Answers1

1
class User < ActiveRecord::Base
  geocoded_by :address

  after_validation :geocode, if: :address_changed?
end

User.near([latitude, longitude], 10).to_json

# or even simpler since current_user is a geocoded object (i.e has lat/long)

User.near(current_user, 10).to_json
Damien
  • 26,933
  • 7
  • 39
  • 40
  • I have tried this and it shows error as NoMethodError (undefined method `near' for #) – logesh Sep 11 '13 at 11:34
  • Did you set up Geocoder for the User class? Will update my answer if not. – Damien Sep 11 '13 at 11:35
  • No i have not. How should i set up? – logesh Sep 11 '13 at 11:36
  • Updated. How did you calculate the lat and long for your users? Do they have an address field? – Damien Sep 11 '13 at 11:37
  • No actually this latitude and longitude is sent from the iphone app. – logesh Sep 11 '13 at 11:38
  • Then you don't need the `after_validation` callback. Also don't bother about the argument of `geocoded_by`, this method is there to include the required modules. – Damien Sep 11 '13 at 11:45
  • so should i add the line geocoded_by :address alone in my user model or do i have add to add something as well – logesh Sep 11 '13 at 11:47
  • Yes, you have to add `geocoded_by :something` in your model. It includes/extends the modules for the `near` class methods (and some others). – Damien Sep 11 '13 at 11:50
  • Thank you so much. It works and i will accept your answer after checking completely. – logesh Sep 11 '13 at 12:16