1. Geocoding in Ruby

    The Geocode Module

    Have you ever needed to turn a partial address into a full one? Have you ever wanted to get the latitude or longitude for a particular address? Maybe you want to plot addresses on a Google Map, or maybe, like a colleague of mine, you find it redundant to ask for city, state AND zip when creating a form. If any of these apply to you, you might be interested in geocoding.

    Geocoding is the process of turning an address (or in some cases a partial address) into latitude and longitude. Often times during the process you get more information about the address. For instance if you geocode “1000 5th Ave, 92101”, not only with you get the latitude and longitude (32.715714, -117.160158), you also find out that it’s in San Diego, CA.

    There are many services out there that will geocode addresses for you. For instance the Cartographer plugin provides a couple of nice Geocoding modules. One for Geocoder.us and one for Ontok. Both are great alternatives for geocoding. However, I wanted to use Google, and I didn’t want to use the full Cartographer plugin. So I wrote the following module that allows you to geocode addresses in Ruby using Google’s service.

    You’ll notice a few returned items that are always nil, that’s because it is a drop in replacement for the Geocoding modules in Cartographer. So even if you use the Cartographer plugin, you can still use this module.

    Using the Geocode Module

    To use the Geocode module, simply include it, and pass it an address or partial address.