Having to geolocalize our users and not requiring a high level of accuracy, we decided to explore the IP geolocalization of the request instead of using the native API of the browser.
We found and tried a couple of gems:
Geo_ip retrieves a geolocation of an IP address using the ipinfodb.com api. You need to register for an API key, but after having obtained it and set into your app, you are set.
You just need to add in your controller
location = GeoIp.geolocation(request.remote_ip)
location you’ll have a hash with the latitude and longitude, the Country, the City, and the zip code.
Geokit-rails uses the geokit gem to offer a set of location-based features for your app, not only IP geolocalization. The list includes ActiveRecord distance-based queries, distance calculations and geocoding.
As for the IP geocoding, the code to use is the following:
location = Geocoders::MultiGeocoder.geocode(request.remote_ip)
the interesting part is that geokit is IP geocoder-agnostic since it provides a failover aong multiple IP geocoders and it queries them using the order you provide. Please, check the Geokit documentation to see the configuration options.
You can also write your own geocoder!
A special mention goes to the rubygeocoder gem, which we didn’t use for our app (it seems it doesn’t behave well with Heroku, where our app was being served from) but it does look interesting nonetheless.