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