Wednesday, October 22, 2008

Geocoding... in Reverse!

One of the first concepts I explain to developers that are new to mapping APIs is geocoding: the process of converting an address into a latitude/longitude pair. Approximately 99.9% of developers will have to use a geocoder before they can get their data onto a map, and to let users of their map locate themselves. Thankfully, we offer both the HTTP Geocoding service for server-side geocoding (the former), and the GClientGeocoder class for client-side geocoding (the latter).

Now, let me introduce the more advanced topic of reverse geocoding: the process of converting a latitude/longitude pair into an address. A much smaller (but important) percentage of developers will want to use a reverse geocoder to let their map users know the address for a particular point on the map, perhaps to help them fill in a form faster (why type when you can click?!). For those developers, we're now pleased to offer address-level reverse geocoding support to both our HTTP service and the GClientGeocoder class. To make it super easy to use, the interface for reverse geocoding is nearly the same as forward geocoding - the only difference is sending in a lat/lng instead of an address. Here's a code snippet for doing client-side reverse geocoding, taken from this sample:

geocoder.getLocations(latlng, function(addresses) {
  if(addresses.Status.code != 200) {
    alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
  } else { 
    var result = addresses.Placemark[0];
    map.openInfoWindow(latlng, result.address);
  }
});

As a live demo of the reverse geocoder, check out MeetWays, a site that mashes up the Google AJAX Local Search API with the Google Maps API and just a bit of math. It calculates the point between two addresses on the map, uses the reverse geocoder to find out the address for that point, and then does a local search near that address to find places to meet that are halfway between. Since reverse geocoding works in all the places that forward geocoding works, then you should be able to meet up with your friends in more than 70 countries.

screenshot of Meetways

For more information on this new service, read through the reference and sample code. We'd love to see the ways that developers will use the reverse geocoder, so please post a link to your reverse geocoding demo or website in the forum. Now go forth and reversify!