Searching location by IP using Geocoder gem not working properly with rails 4

二次信任 提交于 2019-12-24 12:34:32

问题


I have added geocoder gem in my rails application but it is not responding properly when i am searching location by IP.

The problem is that it's returning nil array.

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

But sometimes it's working, I don't know why.

What I have done:

Gemfile

gem 'geocoder'

initializers/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:


was getting the same error used this " Geocoder.configure(ip_lookup: :telize) " it solved the problem. source - https://github.com/alexreisner/geocoder/issues/770



来源:https://stackoverflow.com/questions/27519219/searching-location-by-ip-using-geocoder-gem-not-working-properly-with-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!