Skip to content
Snippets Groups Projects
README.rdoc 7.93 KiB

Geocoder

Geocoder adds object geocoding and database-agnostic distance calculations to Ruby on Rails. It’s as simple as calling fetch_coordinates! on your objects, and then using a scope like Venue.near("Billings, MT"). Since it does not rely on proprietary database functions finding geocoded objects in a given area works with out-of-the-box MySQL or even SQLite.

Geocoder is compatible with Rails 2.x and 3.x. This is the README for the 3.x branch. Please see the 2.x branch for installation instructions, documentation, and issues.

1. Install

As a Gem

Add this to your Gemfile:

gem "rails-geocoder", :require => "geocoder"

and run this at the command prompt:

bundle install

Or As a Plugin

At the command prompt:

rails plugin install git://github.com/alexreisner/geocoder.git

2. Configure

A) Add latitude and longitude columns to your model:

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

B) Tell geocoder where your model stores its address:

geocoded_by :address

C) Optionally, auto-fetch coordinates every time your model is saved:

after_validation :fetch_coordinates

Note that you are not stuck with the latitude and longitude column names, or the address method. See “More On Configuration” below for details.