开发者

Geocoder Gem Reverse Geocoding

After watching RailsCasts #273 I want to use the Geocoder gem. I've seen this:

class Skatepark < ActiveRecord::Base
  reverse_geocoded_by :latitude, :longitude
  after_validation :fetch_address
  ...
end开发者_开发知识库

which will reverse geocode the coordinates and populate :address with formatted_address.

Can I get this separated as :street, :locality, :region, :country, and :postal_code from the geocoder gem?


I don't know your model but this is how you populate it. It's also documented in the page you referred to.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜