如何使用 gmaps4rails 尽可能进行地理编码并跳过错误?

发布于 2025-01-05 07:54:02 字数 507 浏览 5 评论 0原文

我在当前的项目中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2025-01-12 07:54:02

您是否只是尝试过:

acts_as_gmappable :validation => false

Did you simply try:

acts_as_gmappable :validation => false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文