使用Geocoder gem通过IP搜索位置与rails 4无法正常工作

时间:2014-12-17 05:57:34

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

我在我的rails应用程序中添加了geocoder gem,但是当我通过IP搜索位置时,它没有正确响应。

问题是它正在返回nil数组。

Geocoding API's response was not valid JSON.
[]

但有时候它有效,我不知道为什么。

我做了什么:

的Gemfile

gem 'geocoder'

初始化/ geocoder.rb

Geocoder.configure(
    :timeout  => 20,
    :lookup   => :google,
    #:ip_lookup => :google,
    #:api_key  => "xxxx-xxx",
    :units    => :km
)

application_controller.rb

def current_location
 if Rails.env.development?
  geo_data = Geocoder.search('50.131.44.114')
 else
  geo_data = Geocoder.search(request.remote_ip)
 end
 [geo_data[0].city, geo_data[0].country_code ].compact.join(', ') 
end

1 个答案:

答案 0 :(得分:0)

得到了同样的错误 用过这个 “Geocoder.configure(ip_lookup :: telize)”它解决了这个问题。 来源 - https://github.com/alexreisner/geocoder/issues/770