Quick and simple geocoding without external librairies

29/09/2010

When you want to implement geocoding for one of your Ruby on Rails models, the first thing you may do is to include the good old geokit gem…

But do you know that if you just want to retrieve and store lat/lng in your records, geokit or another external library is unnecessary ?
And moreover, geokit still uses the old Google Maps API v2… Google has updated his Maps API (v3) and it is very easy to consume it using simple GET calls returning JSON. And, icing on the cake : Google API v3 does not require an API key! So no more headache managing multiple API keys for all your environments…

Here is a small example (geocode the street field). This is a Mongoid model, but of course it would work with ActiveRecord or anything else…