
Google Maps has a well know API that can be used for great things. The most common one of course – putting a map on your website and placing markers to show where things are. One thing I never realised is that as well as the standard Javascript API that is most commonly used, Google offers a Web Service that offers a lot of the functionality but can be used on the server-side. It’s this web service functionality that will provide us with free UK postcode lookup, something you normally have to pay for due to Royal Mail’s restrictive copyright policies.
Note: In the following scripts I am assuming use of the Google Maps API Version 3.0
The Google Maps Web Service is a REST based service. This means that it uses standard HTTP protocols to communicate and therefore it makes it really easy to integrate with. REST services don’t have any specific return format, and in the case of this one Google has chosen to return either XML or JSON formats depending on what is needed.
REST opens up specific URLs to send a standard HTTP GET request to get the data required. In the case of the Google Maps Web Service, it opens up URLS that look something like the following:
1
http://maps.googleapis.com/maps/api/service/output?parameters
For more information on the Google Web Service format and requirements take a look at the documentation. Requests to the Geocoding API that we are going to use in our code doesn’t require any signing or client IDs and is available to be used by anyone that is able to script it correctly. Of course, as everything free, it comes with usage limits and terms and conditions.
The trick to postcode lookup is geocoding the location from postcode into full address. Google makes this possible with the Geocoding API. As described earlier, the API takes the form of a REST web service that offers a JSON or XML response. The format of the request needs to take the format of:
1
http://maps.googleapis.com/maps/api/geocode/output?parameters
“output” should be replaced with the output format we would like (XML or JSON) in lowercase. ”parameters” should be replaced with the parameters involved in our geocoding request – in our case we are just going to worry about the “address” parameter but there are others you can use. One thing to remember is you ALWAYS have to pass in a “sensor” parameter that is either set to true or false, this defines whether or not the web service is to use the client’s built in GPS sensor when it makes the request. In the case of this post, I am always going to set it as false because I am requesting from a server which doesn’t have a GPS sensor and, is in a different location to the client’s machine so would be inaccurate anyway.
(I assume that) Due to Royal Mail’s copyright over the postcode database, Google is unable to perform a 100% accurate postcode lookup for free. Therefore, we can’t return the “full” address from the postcode passed in, but we can return everything except for the first line. So, this just means that instead of capturing “House number” and “postcode” as is common, we will have to capture “First line of address” and “postcode”. So, let’s take a look at how we find the full address for “10 Downing Street, SW1A 2AA”. Here is the REST URL we will use (remember to urlencode() your URLs):
Note: I am going to request an XML response from now on as it is easy to use in PHP
1
http://maps.googleapis.com/maps/api/geocode/xml?address=10+Downing+Street,+SW1A+2AA&sensor=false
I am not going to post the response as it is quite long, if you want to take a look at it, just paste the URL into your browser and then take a look at the source.
The response that is received contains the information we need to geocode our postcode into a full address. The first step is to parse the XML using the simpleXML function within PHP:
1 2 3 4 5 6 7
<?php $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=10+Downing+Street,+SW1A+2AA&sensor=false'; $parsedXML = simplexml_load_file($url); ?>
This will give us a simpleXML object that can be used to pull out the information we require. Firstly, lets check if we have received the ‘OK’ status within the response.
1 2 3 4 5 6 7 8 9
<?php
if($parsedXML->status != "OK") {
echo "There has been a problem: " . $parsedXML->status;
}
?>
Once we have checked the response from Google is has an “OK” status then it means it contains the information we want and can be retrieved. In general, because we have passed in a postcode we will get a single address in the response, I will assume this in the following code. The steps needed to get our our address information is to loop through our address_component elements and retrieve the information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
$myAddress = array();
foreach($parsedXML->result->address_component as $component) {
if(is_array($component->type)) $type = (string)$component->type[0];
else $type = (string)$component->type;
$myAddress[$type] = (string)$component->long_name;
}
print_r($myAddress);
?>
The output of this looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Array
(
[street_number] => 10
[route] => Downing St
[locality] => London
[administrative_area_level_3] => Westminster
[administrative_area_level_2] => Greater London
[administrative_area_level_1] => England
[country] => United Kingdom
[postal_code] => SW1A 2
)
As you can see, I have decoded my postcode into a full address very easily and most importantly, it’s FREE! It is just as easy to use the web service through AJAX and getting it to return a JSON object to put it straight into a form. For more information on the Google Geocoding Web Service, take a look at the documentation, it has a full list of the fields you will receive in the response.
If you have found this useful, please take a look at my older posts and sign up to my RSS feed to get all my future posts sent straight to you. Thanks for reading!
This is the second in a series of posts about how I built Doqumentor – The Runtime PHP Documen
I have recently spent a bit of time writing a new PHP development tool for documenting code at runti
The iPhone has had an accelerometer in it since the start, allowing users to tilt the device and it
[...] This post was mentioned on Twitter by puremango.co.uk, Murray Picton. Murray Picton said: Free UK postcode lookup! http://www.murraypicton.com/2010/12/free-uk-postcode-lookup-using-the-google-maps-api/ [...]
This is not a post code look up
If you were to do this
http://maps.googleapis.com/maps/api/geocode/xml?address=SW1A+2AA&sensor=false
you will not get the street name which is what you want with a postcode look up after all.
GeocodeResponse>
OK
−
postal_code
Westminster, London SW1A 2AA, UK
−
SW1A 2AA
SW1A 2AA
postal_code
−
London
London
locality
political
−
Westminster
Westminster
administrative_area_level_3
political
−
Greater London
Greater London
administrative_area_level_2
political
−
United Kingdom
GB
country
political
−
SW1A
SW1A
postal_code
−
−
51.5035397
-0.1276952
APPROXIMATE
−
−
51.5003921
-0.1308428
−
51.5066873
-0.1245476
Hi Flukywotsit,
Thanks for your comment, it is 100% accurate that this is no true postcode lookup. As explained in the post, due to Royal Mail’s restrictive licensing of the official postcode database, Google can’t offer a street level postcode lookup. What this does offer however is the chance to lookup the majority of a person’s address from them entering their postcode and I think this is a great feature – especially for free.
I Agree, this is a fantastic alternative to using the postcode look ups via Royal Mail.
At least the customer can have most of their address filled out for them to speed up registration / checkout, which i feel is the most important thing.
And, if you getting several thousand look ups per month, it could end up costing thousands over the year to use Royal Mail’s database, at approx 1p per look up.
Thanks for the info, i wouldn’t have even thought about using the Google API for look ups !!
Nice work with processing the results of the API. I just thought I’d share something with you that I saw on the Google Maps API documentation:
“the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited.”
Which means that that you have to display the results of your geocoding lookup on a Google map. Might be an important thing to remember just in case Google turn around and shake a finger
Thanks for your great post. Mark is correct in the fact that the guidelines for using Googles API without paying any license fee is that the information is used in conjuction with other google visible products such as maps.
I developed part of the Google Maps system and you should be aware that if you are using the service as a postcode lookup, it may be possible that one day your code just stops working. So beware.
I agree that the royal mail postcode database information is very expensive and although there are loads of companies out there who offer a lookup service, they all can be costly, especially when you dont know if you are actually going to make any money from people using your site.
But thanks again for your great post, very useful to all developers out there, but just make sure to have some form of backup plan if Google pulls the plug.