您可以使用geokit和geokit-rails gems 来实现这一点。有关文档,请参见此处:https ://github.com/imajes/geokit-rails
基本上,它的工作原理是这样的:您保存用户的地址数据,然后使用地理编码服务(例如 Google Maps 或 Yahoo Placefinder)查找该地址并将其映射到空间中的一个点(lat/lng)。然后可以使用这些点来计算距离等。
一个例子:
class User < ActiveRecord::Base
  # has the following db fields:
  # - city
  # - state
  # - country
  # - latitude
  # - longitude
  acts_as_mappable :default_units       => :miles, 
                   :default_formula     => :sphere, 
                   :lat_column_name     => :latitude,
                   :lng_column_name     => :longitude,
                   :auto_geocode        => {:field => :full_address}
  def full_address
    [city, state, country].reject(&:blank).join(', ')
  end
end
然后您可以执行以下操作:
# find all users in a city (this has nothing to do with geokit but is just a normal db lookup)
User.where(:city => 'New York')
# find all users that are within X miles of a place
User.find_within(300, 'Denver')
还有更多,只需查看文档...
此示例向您展示如何使用geokit gem。这个宝石似乎不再处于积极开发中。所以也许检查一下地理编码器是值得的:https ://github.com/alexreisner/geocoder