Google Maps API Tutorial

© 2010 Mike Williams

 
Translate this page:

 

UK Posctcodes

The Problem

Since I wrote this page, The Problem seems to have gone away making this page unnecessary

Unlike other countries, the geographical information associated with UK postcodes is strictly controlled. The copyright is owned by the Royal Mail, who make a lot of money selling licences to use it.

For example, I just bought a new TV. The guy at the shop didn't ask for my address, but just asked for my postcode and house number, and used that to find my full street address. The shop presumably finds it worthwhile fo pay the annual £85 licence per workstation, or the annual £75,000 corporate licence for all the workstations operated by that chain of stores, or one of the intermediate licences. Since large numbers of UK high street shops do the same thing, that adds up to an awful lot of money that the Royal Mail receive from licensing that data. Therefore they're not keen to allow Google to give the information away for free via the API geocoder.

The Royal Mail have allowed Google to buy a licence to use the data on its own websites, but not to redistribute the data to other people.

This results in it being possible to find the exact location of "FY2 0HH, UK" at maps.google.com, but if you try the same search with the API geocoder you get a point that's nearly a mile away.

What actually happens

The API geocoding database contains the locations of places like "FY2 0" and "FY2 9". I.e. the "outcode" part of the postcode plus the numeric part of the "incode".

When you reverse geocode a point, it gives the correct one of those locations as the Locality.PostalCode.PostalCodeNumber

When you forward geocode a UK postcode, the geocoder is only allowed to look at the "outcode" part. It then returns an arbitrary one of the "FY2 ?" locations, not necessarily the correct one. Which one of those locations actually gets returned is influenced ny things like country bias. There is a suggestion that it might even be biassed by the location of the user who is calling your page.

Workrounds

Use the full address

You could always ask your user to input their full address. That generally works.

The terms of the licence require the geocoder to discard the "incode" part of the postcode, and effectively performs a search for [street address] near [outcode]. In some extreme cases, even that won't work. An example near where I live is "10 Bispham Road FY6 7PE, UK". There are three "Bispham Road"s that are near FY6. One at "FY3 7HJ". One at "FY5 1DG" and the one that I asked for at "FY6 7PE". The API geocoder returns all three, but the one I asked for is the second placemark.

Use a non-google geocoding service

I don't know of any commercial on-line services, but I'm sure there are some.

Buy the data from the Royal Mail

Put it on your server and perform your own lookups. I reckon that you need to pay the annuall £3,850 fee for a System Licence to do that.

Use the Google Local Search API

I've no idea how the licensing of this works, but calls to the Google Local Search API do correctly geocode full UK postcodes.

A Google spokesman did once say that this might not always be the case, so if you do use this strategy don't be surprised if it suddenly stops working.

It's not a good idea to use Google Local Search as a general purpose geocoder. It's slower than the API geocoder and can sometimes get confused between placenames and business names. So if there's a possibility that your search string might not be a UK postcode, it's a good idea to test it first, see below.

Use GDirections

I've no idea how the licensing of this works, but calls to GDirections do correctly geocode full UK postcodes. The locations are slightly different from the maps.google.com locations, since they will have been snapped to the nearest street.

The strategy is to request a route from the postcode to itself, like "from: FY2 0HH to: FY2 0HH", wait for the reply, then use .getPolyline().getVertex(0) to obtain the location.

If you do use this strategy don't be surprised if it suddenly stops working.

It's not a good idea to use GDirections as a general purpose geocoder. It's slower than the API geocoder, has a lower daily quota, and can only handle one request at a time. So if there's a possibility that your search string might not be a UK postcode, it's a good idea to test it first, see below.

There's also considerable doubt about whether this strategy is legal. Read section 10.12 of the Terms for yourself before implementing this strategy.

Testing if a string is a UK postcode

To test if a string looks like a UK post code you can use this regexp
^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$.

That expession assumes that the string is in upper case, has exactly one space between outcode and incode, has no leading or trailing whitespace and no punctuation. That's a bit more severe than we need, since GDirections and Google Local Search will accept any case and any punctuation and whitespace, so you might want to tidy it up first before performing the test.

That expression should be sufficient to distinguish a full UK postcode from anything else, but if you want to be more exact, there's a long form in the Wikipedia. Don't use the very long expression from the Wikipedia article that includes BFPO postcodes, because BFPO postcodes don't represent fixed geographical locations, but groups of British Overseas Forces which may well be mobile.

Example

Here's an example that tests whether a search string is a UK postcode, and if so uses the GDirections trick rather than the GClientGeocoder to find the location.

Back to Mike's Homepage
Up to the start of this tutorial