如何使用 gmaps4rails 尽可能进行地理编码并跳过错误?
我在当前的项目中使用 gmaps4rails Awesome gem,并且必须在数据库中导入一千多个应该充当 gmappable 的对象。
然而,并非所有的地址都有正确的地址!因此,当我尝试导入时,它在无法使用 Gmaps4rails::GeocodeStatus 进行地理编码的第一个地址上失败。
是否可以在可能的情况下跳过错误并进行地理编码?
我想出了这个:
acts_as_gmappable :process_geocoding => false
before_save :prepare_gmaps
private
def prepare_gmaps
begin
data = Gmaps4rails.geocode(address).first
self.latitude= data[:lat]
self.longitude= data[:lng]
rescue Gmaps4rails::GeocodeStatus
end
end
I'm using gmaps4rails awesome gem in my current project and have to import more than a thousand objects in the DB that should act as gmappable.
However not all of them have correct addresses! So when I try to import it fails on the first address that could not be geocoded with Gmaps4rails::GeocodeStatus.
Is it possible to skip errors and geocode where possible?
I came up with this:
acts_as_gmappable :process_geocoding => false
before_save :prepare_gmaps
private
def prepare_gmaps
begin
data = Gmaps4rails.geocode(address).first
self.latitude= data[:lat]
self.longitude= data[:lng]
rescue Gmaps4rails::GeocodeStatus
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否只是尝试过:
Did you simply try: