Jam*_*ier 4 ruby-on-rails rails-geocoder
该地址解析器创业板将自动反向上保存如果行地址解析after_validation :reverse_geocode包含在模型中.这导致一长串文本被保存为地址,但格式类似于"街道名称,城市名称,县名,州名,邮政编码,国家名称".
我只对这个特定项目的街道名称感兴趣,所以我想知道是否有办法修改after_validation调用只保存该信息.
如果我手动执行反向地理编码,我可以访问road结果中的值:
place = Place.first
result = Geocoder.search("#{place.latitude},#{place.longitude}")
street = result[0].data['address']['road']
Run Code Online (Sandbox Code Playgroud)
我可以设置我自己的自定义after_validation,但是如果geocoder已经提供了这个,我宁愿不重复功能.
或者,如果有一种完全不同的方式,我可以将纬度/经度转换为街道名称,我有兴趣听到它.如果此选项还包括地址编号或地址范围,则可以.
您可以通过提供一个使对象进行地理编码的块和一个Geocoder :: Result对象数组来自定义reverse_geocode方法.
reverse_geocoded_by :latitude, :longitude do |obj,results|
if geo = results.first
obj.street = geo.address
end
end
after_validation :reverse_geocode
Run Code Online (Sandbox Code Playgroud)
每个Geocoder :: Result对象result都提供以下数据:
result.latitude - float
result.longitude - float
result.coordinates - array of the above two
result.address - string
result.city - string
result.state - string
result.state_code - string
result.postal_code - string
result.country - string
result.country_code - string
Run Code Online (Sandbox Code Playgroud)
可以在地理编码文档中找到更多信息.您甚至可以找到更多可以从Geocoder::Result对象中删除的字段.
| 归档时间: |
|
| 查看次数: |
2615 次 |
| 最近记录: |