地理编码 API 响应速度不够快

Geocoding API not responding fast enough

我正在使用 Geocoder gem,但最近它似乎不起作用。

我收到这个错误:

Geocoding API not responding fast enough (use Geocoder.configure(:timeout => ...) to set limit).

我的application_controller.rb是:

before_filter :lookup_ip_location

  private

    def lookup_ip_location
      if Rails.env.development?
        prr = Geocoder.search(request.remote_ip).first
        p "this is #{prr.inspect}"
      else
        request.location
      end
    end

这是development.rb:

# test geocoder gem locally
  class ActionDispatch::Request
    def remote_ip
      "71.212.123.5" # ipd home (Denver,CO or Renton,WA)
      # "208.87.35.103" # websiteuk.com -- Nassau, Bahamas
      # "50.78.167.161" # HOL Seattle, WA
    end
  end

我正在从 development.rb 加载 IP 地址以检查地理编码器是否在本地工作,但它没有。我收到上述错误。

此外,当打印 prr 时,我得到 nil。

我还添加了一个 geocoder.rb 初始化程序以将超时时间提高到 15 秒,但即使在浏览器加载页面 15 秒后我仍然收到相同的消息。

它坏了吗?我应该使用另一个 gem 吗?如果有,您有什么建议吗?

有趣。我尝试了您的确切方法,运行遇到了同样的问题。我还尝试将超时时间提高到 60 秒,但出现了同样的错误。

然后我注意到 Geocoder 使用 freegeoip。所以我去看看那是怎么回事。瞧,freegeoip.net 已关闭。可疑的。

所以我检查了 Geocoder documentation 他们提供的任何不同的 IP 地址查询服务。果然,"Ip Address Services"下,有多个offer。我尝试了第一个不需要 API 键的,它是 :ipinfo_io.

[18] pry(main)> Geocoder.configure(ip_lookup: :ipinfo_io)
=> {:timeout=>30,
 :lookup=>:google,
 :ip_lookup=>:ipinfo_io,
 :language=>:en,
 :http_headers=>{},
 :use_https=>false,
 :http_proxy=>nil,
 :https_proxy=>nil,
 :api_key=>nil,
 :cache=>nil,
 :cache_prefix=>"geocoder:",
 :basic_auth=>{},
 :logger=>:kernel,
 :kernel_logger_level=>2,
 :always_raise=>[],
 :units=>:mi,
 :distances=>:linear}
[19] pry(main)> Geocoder.search("144.138.175.101")
=> [#<Geocoder::Result::IpinfoIo:0x007fce5da5fe28 @cache_hit=nil, @data={"ip"=>"144.138.175.101", "city"=>"", "region"=>"", "country"=>"AU", "loc"=>"-27.0000,133.0000"}>]

而且有效!但是响应没有太多信息。我建议查看 Geocoder 使用的其他 ip 查找服务。找一个可靠的并且有足够的响应信息来满足您的需求。似乎 freegeoip 是免费的,但也可能不可靠。干杯。

编辑:找到了一些关于 freegeoip.net here 的相关信息。如果你真的想使用 freegeoip,看起来你可以 运行 你自己的实例。希望这对您有所帮助!