Geocoding in Rails with the Geocoder Gem

If you are building a property rental application or a transportation startup and using maps heavily, then the Geocoder gem would be your best friend.



What is the Geocoder gem for?

Geocoder gem is a geocoding solution for Ruby. With Rails it adds geocoding (by street or IP address), reverse geocoding (finding street address based on given coordinates), and distance queries.

What is geocoding?

Geocoding is the process of transforming a description of a location—such as a pair of coordinates, an address, or a name of a place to a location on the earth’s surface.

How do you install Geocoder gem on your rails application?

Install Geocoder like any other Ruby gem:

  gem install geocoder

Then run bundle install.

How do you use Geocoder in ActiveRecord?

The model that needs Geocoding must have the two attributes for storing latitude and longitude. Generate a migration to add the attributes to your target model:

rails generate migration AddLatitudeAndLongitudeToModel latitude:float longitude:float
rake db:migrate

To gecode your model, you must provide a method that provides an address. Then let geocoder the which method returns the the address.

geocoded_by :full_street_address        # Letting Geocoder gem know which method returns an address
after_validation :geocode               # auto-fetch coordinates
Location.near('Omaha, NE, US', 20)       # venues within 20 miles of Omaha
Location.near([40.71, -100.23], 20)      # venues within 20 miles of a point
Location.bearing_to(["Paris, France"])

Hooking up Geocoder with Google

Geocoder can be hooked up to different street and IP address geocoding services. I will discuss how you can hook your geocoder gem with google. Although the default lookup for street address uses google, you can increase quota by adding your api key (and adding your Credit Card to your developer account in Google).

To create a Rails initializer with an example configuration, use this command:

rails generate geocoder:config

Your config should look like this:

  # Geocoding options
  timeout: 100000, # geocoding service timeout (secs)
  lookup: :google, # name of geocoding service (symbol)
  # language: :en, # ISO-639 language code
  use_https: true, # use HTTPS for lookup requests? (if supported)
  # http_proxy: nil, # HTTP proxy server (user:[email protected]:port)
  # https_proxy: nil, # HTTPS proxy server (user:[email protected]:port)
  api_key: 'DUMMY_API_KEY', # API key for geocoding service
  # cache: nil, # cache object (must respond to #[], #[]=, and #keys)
  # cache_prefix: 'geocoder:', # prefix (string) to use for all cache keys

  # Exceptions that should not be rescued by default
  # (if you want to implement custom error handling);
  # supports SocketError and Timeout::Error
  always_raise: :all,

  # Calculation options
  units: :mi, # :km for kilometers or :mi for miles
  # distances: :linear # :spherical or :linear

Make sure you add your api key.

Where can you get your google api key?

Access your google developer console.

For more information, visit the official geocoder gem website:

Good luck with using Geocoder gem on your startup!