Geocoding with php

Per Bernhardt20. August 2007

Recently one of our customers asked us to pimp his project database. This database is a pool of european wide projects in non-formal learning and youth work. The customer wanted the projects to be shown on a map of europe to emphasize their extensiveness. Unless we wanted somebody to put each of the over 500 projects on the map manually, we had to find a way to use a geocoder, because we had nothing more than a description text including address information like "Athens, Greece".

A geocoder is a piece of software or a (web) service that helps with geocoding. Amongst others geocoding is the process of assigning street addresses to geographic coordinates, and vice versa. In other words a geocoder is a nice little helper that enables you to map an address from its simple string representation to its exact coordinates.

Certainly everybody who has ever used Google Map API knows it's geocoder. The Google Map API lets you embed Google Maps in your own web pages with JavaScript. It provides a class called GClientGeocoder that can translate address strings to coordinates of latitude and longitude by sending an asynchronous HTTP request (AJAX) to Google's servers. Unfortunately that was not a feasible solution, because sending over 500 requests to Google on every page load of the customers site would generate way to much traffic and delay. Therefore we had to be able to save the coordinates into the customers database. Well, anybody out there who does SQL in Javascript?? Of course, not a good idea.
So what we really wanted was a kind of GClientGeocoder for PHP!

Thanks to Google there was nothing more easy than that! Google allows to access the Map API geocoder directly using server-side scripting. You just need to send a request to including the address and the preferred output type and Google answers in either xml, kml, csv, or json. Thanks to PEAR'S HTTP_Request and PHP's database and string handling we could filter the address out of the project description, send it to Google and save the responded coordinates into the project database within several minutes.

You can see the result here: Innovative Long-term Youth in Action Projects

Sometimes everything you need is just there! Only the right approach is missing to get things done easily.