【问题标题】:Skip geocoding in some cases在某些情况下跳过地理编码
【发布时间】:2014-06-25 12:55:34
【问题描述】:

我正在使用 Geocode gem 将地址转换为(对数、纬度)坐标。为此,我在地理编码的用户配置文件模型中添加了这个:

geocoded_by :address

after_validation :geocode, if: Proc.new { |p| p.address_changed? }

这很好用,但是现在,我正在制作一个迁移脚本,用地址以及经度和纬度填充 Profiles 表。在这种情况下,地理编码器仍在进行转换,我多次收到以下警告:

Google Geocoding API error: over query limit.

在某些情况下如何跳过地理编码,例如在我已经有了坐标的迁移中?

【问题讨论】:

  • 谢谢@rene。我会在下一篇文章中介绍这个。

标签: ruby-on-rails-4 rails-geocoder


【解决方案1】:

正确答案取决于您如何实现address_changed?

推荐的是:

after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }

但我倾向于使用:

after_validation :geocode, if: ->(obj){ obj.address.present? and !obj.address.latitude? and !obj.address.longitude? }

在这种情况下,如果已设置 latitude,longitude 对,您将跳过地理编码。

【讨论】:

    【解决方案2】:

    config/initializers/geocoder.rb 中输入以下代码:

    Geocoder::Configuration.lookup = :yandex
    
    for update to quota: 25000 requests / day
    

    更多详情here.

    【讨论】:

      猜你喜欢
      • 2015-02-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      相关资源
      最近更新 更多