GMap Geocoding UK Postcodes

If you search Google Maps with a UK postcode, it'll take you straight to the right location. However, if you want to use the Google Maps API in your own application, trying to use GClientGeocoder's getLatLng to convert the postcode into longitude and latitude will fail with a code 603 error (G_GEO_UNAVAILABLE_ADDRESS) indicating that legal or contractual reasons prevent Google from returning the information. Japan and China have the same restrictions as the UK, according to Mapki; detailed geocoding is available for Canada, France, Germany, Italy, Spain, and USA; other countries get geocoding for country and city names.

This restrictive licensing is because both the Royal Mail (postcodes→long/lat) and Ordnance Survey (addresses→long/lat) hold the copyright on that information and have licensed it to Google under the condition that it not be made available for other purposes. The licensing fees are large, which is why the Royal Mail has been forced to reduce its licensing prices for small users, which it's doing slowly, over the next 3 years.

OpenStreetMap and FreeThePostcode have been doing admirable work to build up a free database of locative information which, arguably, should have been freely available to start with. However, there's still a lack of data for anyone who needs to geocode UK postcodes today. Luckily, Emad Fanous has a wonderful Javascript API for retrieving the longitude and latitude from normal Google Maps searches, rather than through the API. You give it the postal code and country, and it returns a javascript function call that manipulates an embedded map directly. Add use=google2 if you're using the more recent GMap2 API (setCenter(long, lat, [zoom]) rather than centerAndZoom(lat, long, zoom)), and cb=yourCallBack to pass the results to a callback function. format=json can also be used, to get extra detail in the results.

For example,


<script type="text/javascript">
function gmap_geocode_postcode(postcode){
    var s = document.createElement('script');
    s.src = 'http://geo.localsearchmaps.com/?cb=gmap_map_postcode&use=google2&country=uk&zip=' + postcode;
    s.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(s);
}

function gmap_map_postcode(lon, lat){
    map.setCenter(new GLatLng(lon, lat));
}
</script>

<input type="button" value="lookup postcode (UK)" onclick="gmap_geocode_postcode(document.getElementById('edit-location-postal_code').value); return false;">

Comments

If anyone's interested, I have a complete list of UK postcodes with their associated lat/long (some 2.5 millions entries in total): http://linuxbox.co.uk/postcode_database.php

Posted by: Pete on November 1, 2007 3:32 PM

The Multimap Open API offers UK Postcode lookups. http://www.multimap.com/openapi

Posted by: Anonymous on November 20, 2007 2:34 PM

Note this on the Multimap terms of use though if you're planning to store any geocodes locally - iv. You agree that you will not capture or retain any geocodes produced by the Open API in any way, including an address look-up. If You retain geocodes created by use of the Open API or other Multimap services for any other application outside the Open API, You agree to pay a fee for geocoding and geocodes. The fee charged will be based on the data used, the number of records geocoded and rates customarily charged by Multimap in similar situations.

Posted by: Anonymous on May 25, 2008 2:57 PM

It should be:

function gmap_map_postcode(lat, lon){
map.setCenter(new GLatLng(lat, lon));
}

Because the params were the wrong way around twice, the error cancelled out.

Posted by: David on June 1, 2008 11:58 AM

Technically it doesn't matter what the parameters are called, they could be (x,y), (foo, bar) or (apple, orange) as long as they're passed through to the next function in the right slots.

Posted by: Anonymous on June 17, 2008 10:29 AM

Hello,

Any chance I could download the UK postcode database from you?

Thanks

Posted by: Joe in reply to comment from Pete on August 6, 2008 4:35 AM

Sorry, the Postzon database is Crown copyright, so you have to license it from the Royal Mail.

Posted by: Author Profile Page alf in reply to comment from Joe on August 7, 2008 1:52 AM

All fields are optional, email address will not be shown; no HTML, URLs are automatically hyperlinked.