20 Aug 2007

Geocoding with php

By

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 http://maps.google.com/maps/geo 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.

Comments

Craig 16/12/08

I've been considering doing what you described, but according to the Google Maps TOS, what you did is against the agreement. http://maps.google.com/help/terms_maps.html

"10.12 use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation, the Street View API Documentation, or through written permission from Google (for example, you must not use geocodes obtained through the Service except in conjunction with a Google map, but the Street View API Documentation explicitly permits you to display Street View imagery without a corresponding Google map); or"

"10.3 pre-fetch, cache, or store any Content, except that you may store limited amounts of Content for the purpose of improving the performance of your Maps API Implementation if you do so temporarily, securely, and in a manner that does not permit use of the Content outside of the Service;"

Seems stupid to me, but I figured since this affects my implementation, I would share my thoughts.

Per Bernhardt 06/01/09

Hi Craig,

first of all thank you for sharing your thoughts by commenting on my article. I took some days off for winter holidays so sorry for my late reply.

I think we are not breaking the agreement with Google. As you can see on our customers website, we display the pre-fetched content directly with a corresponding Google map, so 10.12 is fullfilled from our point of view.

Also as I described in the article the only reason for pre-fetching is to improve the performance of our Maps API Implementation. And we do so securely and in a manner that does not permit the use of the Content outside of the Service. So what may not be fullfilled in 10.3 is the word "temporarily".

Actually in a legal context, the word "temporarily" is certainly a word that you can disagree about. We cannot imagine that Google wants us to redo the pre-fetching once a day or whatever timespan you take. So we take the word "temporarily" more in the meaning of "intermediately", because we only store it to use it with a corresponding Google map afterwards.

Thanks again!

Barrett 04/03/09

I recently needed a way to re-submit street address for a project and found this article quite helpful, coupled with the article http://code.google.com/support/bin/answer.py?answer=80200. Which actually provides some code samples too.

Thanks for the help! : )

Post your own comment

Please solve the following arithmetic problem to prevent this form from being abused.

Categories

Archive