【问题标题】:How to find the users within 10miles using latitude and longitude of the user in rails?如何使用rails中用户的经纬度找到10英里内的用户?
【发布时间】:2013-09-11 19:57:10
【问题描述】:

我有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

所以我已经转移到地理编码器。我应该如何使用此地理编码器来查找具有上述条件的用户?请帮我。

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 我已经尝试过了,它显示错误为 NoMethodError (undefined method `near' for #<0x00000102a8f900>
    猜你喜欢
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多