In my previous post I showed you how to create a beautiful Google Map for your site with controls showing Google headquarters. Some might say that this isn’t very useful, I don’t know why they think that but for those that agree here is how to get it to show any postcode or address you like.
I strongly suggest you read my other post first if you don’t know much about the Google Map API (read it here)
The process of turning an address into latitude and longitude coordinates is called Geocoding and Google’s API makes it very easy to do so using a simple request to their server. We will go into the legal side later on in this post but for now lets just say that Geocoding is a server intensive process for Google and they recommend client side caching of added coordinates to avoid re-requesting coordinates if you already have them.
Getting the coordinates is actually very easy. Use the following URL in your browser (making sure to replace your API key and an address)
http://maps.google.com/maps/geo?q= ## ADDRESS OR POSTCODE ## &output=xml&key= ## YOUR API KEY ##
This URL will return some XML (if you have done it correctly) which will contain three pieces of useful information
- Geocode Accuracy
If you know what country your going to be searching in then you can add another argument to the URL telling it the TLD or Top Level Domain (com, uk, etc..) to make it a little easier for Googles Geocoder:
Here’s how you get PHP to send the request:
$ch = curl_init(); $url = "http://maps.google.com/maps/geo?q=" . urlencode($address) . "&output=xml&key=" . $api_key; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $return = curl_exec($ch); curl_close($ch);
Why why why are you using CURL you ask? (what is CURL anyway?) Well..
A fair few server administrators are blocking the use for file handling functions to URLs (fopen, file, file_get_contents, etc..); This is sensible because there are exploits that malicious programmers can use to gain access to your site and start causing trouble. CURL is a little slower (I’m told) but safer (apparently).
The querystring we sent to Google passes the type of return we want (
JSON), the address or postcode (urlencoded string) and of course our API key.
The curl options (specifically CURLOPT_RETURNTRANSFER) mean that whatever comes back from your CURL request can be stored in a variable and worked on (as opposed to echoing to the screen).
Although XML parsing in PHP is really easy (xml_parse_into_struct if your interested), it’s a bit overkill for what we need; Instead specify the output as CSV which returns a simple string that is incredibly easy to use. Something like:
This breaks down into the following:
- Request status code (success is 200, anything else means it’s broken)
- Accuracy of search result (5 is postcode level, higher is better as far as 9 which is premise level)
The easiest way to get split the returned CSV string is the PHP explode function:
$array = explode(',', $csv_string);
This returns an array of values split on the delimiter specified (in this case the comma). Now the latitude and longitude can be accessed at positions 2 and 3 respectively ($array and $array).
So whats next?: Use the code I gave in my previous example to generate a map for an address we actually want by passing the latitude and longitude into my render_map function ($map->render_map($array, $array);).
Some things to bear in mind:
The next step in theory would be to build up a database of geocoded coordinates next to addresses and use Google maps literally everywhere. Unfortunately (to my understanding (May 2008)) the Post Office have licensed their data and Google have to follow through on this. Basically it means that in every country except the UK you can save the coordinates wherever you like whereas here you have to geocode Google’s data on the fly EVERY page load.
If you decide this is unacceptable but don’t want to break the Terms & Conditions of Google’s API you can use a geocoding service and pay per postcode. It costs about 5 pence per geocode (Postcodeanywhere May 2008) and they allow you to store the data wherever you like.
Coming next: A find my nearest … search using google maps
If you want more information on the geocoding process then Googles explanation is here