如何使用rails中用户的纬度和经度查找10英里范围内的用户?

时间:2013-09-11 11:31:07

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 rails-geocoder

我拥有所有用户的latitudelongitude。我必须从当前纬度和经度中找到靠近我(around 10 miles)的用户。而且我必须在json中作为回应。我试图为此配置geokit gem,但它向我显示了一些错误,如

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

所以我搬到了地理编码器。我应该如何处理这个地理编码器以找到具有上述条件的用户?请帮我。

1 个答案:

答案 0 :(得分: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