Easily geo-locate your users using their IP address via hostip.info or maxmind.com. Super simple to setup, super simple to use. I’ve found that the data from HostIP and Max Mind differ slightly. Max Mind (paid) has a more complete and accurate database at the moment, but HostIP is growing. For more information about Max Mind visit www.maxmind.com/app/city and for more information about HostIP visit hostip.info
gem install geo_location
config/initializers/geo_location_config.rb:
unless GeoLocation == nil
# Use Hostip (free) GeoLocation::use = :hostip
end
location = GeoLocation.find('24.24.24.24') # => {:ip=>"24.24.24.24", :city=>"Liverpool", :region=>"NY", :country=>"United States", :country_code=>"US", :latitude=>"43.1059", :longitude=>"-76.2099", :timezone=>"America/New_York"} puts location[:ip] # => 24.24.24.24 puts location[:city] # => Liverpool puts location[:region] # => NY puts location[:country] # => United States puts location[:country_code] # => US puts location[:latitude] # => 43.1059 puts location[:longitude] # => -76.2099 puts location[:timezone] # => America/New_York
config/initializers/geo_location_config.rb:
unless GeoLocation == nil
# Use Max Mind (paid) # For more information visit: www.maxmind.com/app/city GeoLocation::use = :maxmind GeoLocation::key = ‘YOUR MaxMind.COM LICENSE KEY’ # This location will be used while you develop rather than hitting the maxmind.com api # GeoLocation::dev = ‘US,NY,Jamaica,40.676300,-73.775200’ # IP: 24.24.24.24 # Use this IP in development mode (development and testing will give you 127.0.0.1) # GeoLocation::dev_ip = ‘24.24.24.24’
end
GeoLocation::use = :maxmind GeoLocation::key = 'YOUR MaxMind.COM LICENSE KEY' location = GeoLocation.find('24.24.24.24') # => {:ip=>"24.24.24.24", :city=>"Jamaica", :region=>"NY", :country=>"United States", :country_code=>"US", :latitude=>"40.676300", :longitude=>"-73.775200", :timezone=>"America/New_York"} puts location[:ip] # => 24.24.24.24 puts location[:city] # => Jamaica puts location[:region] # => NY puts location[:country] # => United States puts location[:country_code] # => US puts location[:latitude] # => 40.676300 puts location[:longitude] # => -73.775200 puts location[:timezone] # => America/New_York
Copyright © 2010 Chris Your. See LICENSE for details.