Complete geocoding solution for Ruby.
Key features:
- Forward and reverse geocoding.
- IP address geocoding.
- Connects to more than 40 APIs worldwide.
- Performance-enhancing features like caching.
- Integrates with ActiveRecord and Mongoid.
- Basic geospatial queries: search within radius (or rectangle, or ring).
Compatibility:
- Ruby versions: 2.5+ and JRuby.
- Databases: MySQL, PostgreSQL, SQLite, and MongoDB.
- Rails: 5.x, 6.x, 7.x and 8.x.
- Works outside of Rails with the
json
(for MRI) orjson_pure
(for JRuby) gem.
Basic Features:
- Basic Search
- Geocoding Objects
- Geospatial Database Queries
- Geocoding HTTP Requests
- Geocoding Service ("Lookup") Configuration
Advanced Features:
- Performance and Optimization
- Advanced Model Configuration
- Advanced Database Queries
- Geospatial Calculations
- Batch Geocoding
- Testing
- Error Handling
- Command Line Interface
The Rest:
See Also:
- Guide to Geocoding APIs (formerly part of this README)
In its simplest form, Geocoder takes an address and searches for its latitude/longitude coordinates:
results = Geocoder.search("Paris")
results.first.coordinates
# => [48.856614, 2.3522219] # latitude and longitude
The reverse is possible too. Given coordinates, it finds an address:
results = Geocoder.search([48.856614, 2.3522219])
results.first.address
# => "Hôtel de Ville, 75004 Paris, France"
You can also look up the location of an IP address:
results = Geocoder.search("172.56.21.89")
results.first.coordinates
# => [30.267153, -97.7430608]
results.first.country
# => "United States"
The success and accuracy of geocoding depends entirely on the API being used to do these lookups. Most queries work fairly well with the default configuration, but every application has different needs and every API has its particular strengths and weaknesses. If you need better coverage for your application you'll want to get familiar with the large number of supported APIs, listed in the API Guide.
To automatically geocode your objects:
1. Your model must provide a method that returns an address to geocode. This can be a single attribute, but it can also be a method that returns a string assembled from different attributes (eg: city
, state
, and country
). For example, if your model has street
, city
, state
, and country
attributes you might do something like this:
def address
[street, city, state, country].compact.join(', ')
end
2. Your model must have a way to store latitude/longitude coordinates. With ActiveRecord, add two attributes/columns (of type float or decimal) called latitude
and longitude
. For MongoDB, use a single field (of type Array) called coordinates
(i.e., field :coordinates, type: Array
). (See Advanced Model Configuration for using different attribute names.)
3. In your model, tell geocoder where to find the object's address:
geocoded_by :address
This adds a geocode
method which you can invoke via callback:
after_validation :geocode
Reverse geocoding (given lat/lon coordinates, find an address) is similar:
reverse_geocoded_by :latitude, :longitude
after_validation :reverse_geocode
With any geocoded objects, you can do the following:
obj.distance_to([43.9,-98.6]) # distance from obj to point
obj.bearing_to([43.9,-98.6]) # bearing from obj to point
obj.bearing_from(obj2) # bearing from obj2 to obj
The bearing_from/to
methods take a single argument which can be: a [lat,lon]
array, a geocoded object, or a geocodable address (string). The distance_from/to
methods also take a units argument (:mi
, :km
, or :nm
for nautical miles). See Distance and Bearing below for more info.
Before you can call geocoded_by
you'll need to include the necessary module using one of the following:
include Geocoder::Model::Mongoid
include Geocoder::Model::MongoMapper