Skip to content

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…

class Place
  include Mongoid::Document
  field :name
  field :street
  
  field :lat, :type => Float
  field :lng, :type => Float
  
  def do_geocode!
    response = Net::HTTP.get_response(URI.parse("http://maps.googleapis.com/maps/api/geocode/json?address=#{Rack::Utils.escape(street)}&sensor=false"))
    json = ActiveSupport::JSON.decode(response.body)
    self.lat, self.lng = json["results"][0]["geometry"]["location"]["lat"], json["results"][0]["geometry"]["location"]["lng"]
  rescue
    false # For now, fail silently...
  end
end
ree-1.8.7-2010.02 > place = Place.new
 => #<Place _id: 4ca27a360e3b7d62d7000005, name: nil, lng: nil, street: nil, lat: nil> 
ree-1.8.7-2010.02 > place.street = "Paris, France"
 => "Paris, France" 
ree-1.8.7-2010.02 > place.do_geocode!
 => [48.8566667, 2.3509871] 
Publicités

From → rails, ruby

6 commentaires
  1. Hunter Nield permalink

    Great tip. So much lighter that geokit and the other libraries.

    The returned json contains a lot of good data so a fair amount of flexibility using this method.

  2. Rick permalink

    Just remember, you are limited to 2,500 maximum calls per day to the Google Maps API to geocode.

    http://code.google.com/apis/maps/faq.html#geocoder_limit

    • Yeah, this is great and not really new, but unfortunately not something you can do in production unless your need for geo-coding is fairly minimal.

      Love those Google APIs though. They’re fantastic.

  3. This is great. Google’s API is so convenient. For a super lightweight gem that does basically what you demonstrate, plus distance calculations on your stored objects see my rails-geocoder project:

    http://github.com/alexreisner/geocoder

Trackbacks & Pingbacks

  1. Link dump for September 28th through September 29th | The Queue Blog
  2. Trevor Turk — Links for 10-21-10

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s

%d blogueurs aiment cette page :