地理编码器:如果响应超过查询限制

Geocoder: if response is over query limit

如果已达到 API 查询限制,我如何询问 Geocoder gem 地址?

csv.each_with_index do |row, i|
  if !row[:location_1].nil?
    coords = row[:location_1]
    g = Geocoder.address(coords)
    row[:address] = g
  end

  if g.include?('over query limit')
    p "limit hit"
  end
end

我不确定要问哪个对象。错误出现在控制台中:

Google Geocoding API error: over query limit.

但我不确定如何在代码中检查它。有帮助吗?

您可能想将 Geocoder.address 包装在 begin/rescue 块中:

csv.each_with_index do |row, i|
  if !row[:location_1].nil?
    coords = row[:location_1]
    begin
      g = Geocoder.address(coords)
      row[:address] = g
    rescue Geocoder::OverQueryLimitError
      p "limit hit"
    end
  end
end

请注意,一旦您点击此按钮,您可能希望跳出救援子句中的循环,这样您就不会向地理编码 Web 服务发送错误请求。

参见Geocoder errors