Geocoding Goodness

Eric Hodel | Tue, 13 Jun 2006 22:01:00 GMT

Posted in ,

I've implemented ruby bindings for the three major geocoding APIs as gems:

APIgem name
Yahoo!yahoo-geocode
Googlegoogle-geocode
geocoder.usgeocoder-us

And as a bonus, you get a Yahoo! search gem to search either the web or for locations, yahoo-search.

The bindings all have similar APIs, but return different objects depending upon what information the API reliably provides. Yahoo! gives the most information, followed by Google, followed by geocoder.us.

Yahoo!

documentation

require 'rubygems'
require 'yahoo/geocode'

yg = Yahoo::Geocode.new application_id
locations = yg.locate '701 First Street, Sunnyvale, CA'
p location.first.coordinates

(and searching for locations)

documentation

require 'rubygems'
require 'yahoo/local_search'

yls = Yahoo::LocalSearch.new application_id
results, = yls.locate 'pizza', 94306, 2
results.each do |location|
  puts "#{location.title} at #{location.address}, #{location.city}"
end

Google

documentation

require 'rubygems'
require 'google_geocode'

gg = GoogleGeocode.new application_id
location = gg.locate '1600 Amphitheater Pkwy, Mountain View, CA'
p location.coordinates

geocoder.us

documentation
require 'rubygems'
require 'geocoder_us'

gu = GeocoderUs.new 'username', 'password'
p gu.locate('1924 E Denny Way, Seattle, WA')
2 comments

Comments RSS FEED

Hi I’m a new rails user, so please go easy on me. I’ve got the google_geocode working, but I don’t know how to deal with the unknown address exceptions. Can you post some sample code for dealing with that?

Thanks!

Damon

Damon Brodie said 20 days later

You want to use exception handling. This is covered in the Pickaxe 1st Ed and in chapter 5 and chapter 6 of Why’s (Poignant) Guide.

If you’re calling it from a controller you’d write something like this:

def lookup
  gg = GoogleGeocode.new GOOGLE_APP_ID
  location = gg.lookup params[:address]
  # do whatever it is you would do
rescue GoogleGeocode::AddressError
  flash[:message] = "I'm sorry, I couldn't find #{params[:address]}" 
end
Eric Hodel said 20 days later

Comments are disabled