Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the geocoder gem for rails, how do i get the visitor's city?

https://github.com/alexreisner/geocoder

I can't find documentation to show the visitor's city from ip address? I know how to with HTML5, but I would like to use the value geocoder has. Thanks

like image 862
kinet Avatar asked Apr 09 '12 14:04

kinet


2 Answers

It took a couple of hours for me to realize that the answer is pretty simple:

request.location.city

Geocoder page has this example but I thought that I would need to declare request but no, nothing more is needed.

If you don't have Geocoder you have to install the gem but nothing besides that.

like image 59
Claudio Shigueo Watanabe Avatar answered Sep 22 '22 06:09

Claudio Shigueo Watanabe


How I did it was -

location = Geokit::Geocoders::IpGeocoder.geocode(source_ip)
city = location.city
country = location.country

Here, source_ip is a string object.

like image 31
mridula Avatar answered Sep 26 '22 06:09

mridula