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;">