看完RailsCasts #273我想使用 Geocoder gem 。我看过这个:
class Skatepark < ActiveRecord::Base
reverse_geocoded_by :latitude, :longitude
after_validation :fetch_address
...
end
这将对坐标进行反向地理编码并使用 formatted_address 填充 :address
。
我可以将其分隔为 :street
、:locality
、:region
、:country
和:postal_code
来自地理编码器 gem?
最佳答案
我不知道您的模型,但这就是您填充它的方式。它也记录在您提到的页面中。
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
关于ruby-on-rails - Geocoder Gem 反向地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6576126/