Geocoder Gem Reverse Geocoding

时间:2011-07-04 21:15:26

标签: ruby-on-rails ruby reverse-geocoding rails-geocoder

观看RailsCasts #273之后我想使用Geocoder宝石。我见过这个:

class Skatepark < ActiveRecord::Base
  reverse_geocoded_by :latitude, :longitude
  after_validation :fetch_address
  ...
end

将反转对坐标进行地理编码并使用formatted_address填充:address

我可以将此分隔为地理编码器gem中的:street:locality:region:country:postal_code吗?

1 个答案:

答案 0 :(得分:5)

我不知道你的模型,但这就是你填充它的方式。它也记录在您引用的页面中。

class Skatepark < ActiveRecord::Base
  reverse_geocoded_by :latitude, :longitude do |obj, results|
    if geo = results.first
      # populate your model
      obj.city    = geo.city
      obj.zipcode = geo.postal_code
      obj.country = geo.country_code
    end
  end
  after_validation :fetch_address
  ...
end