Searching location by IP using Geocoder gem not wo

2019-08-02 23:08发布

问题:

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