The big problem with calculating distances based on postcodes (in the UK) is that all the postcode data is contained in the PAF (Postcode Address File) which is owned by Royal Mail and for which they charge £4000 (four thousand !) pounds a year. And as Royal Mail has just been flogged off to a private company, you can guarantee that this price will increase on an annual basis by at least 10% in order to generate cash for the shareholders.

There are quite a few pirate PAFs out on the internet that community minded folks have produced. But how do you know they are accurate ? And having to maintain a database with millions of postcodes in, so that you can look a few up now and then to read out their lat and long seems like massive overkill. There does not seem to be a direct relationship between postcodes and the OS grid reference system - at least I cannot find one on the Internet - so it's not possible to calculate the lat & long directly from the postcode.

The OS (Ordnance Survey) now produce some free stuff, after years of pressure to stop charging for their map data. None of it is of any direct use for geotagging. The best you can do is to cobble together a monster lookup table (1,600,000 post codes in the UK) from the various bits and pieces you can glean from the OS. And how do you keep it up to date ?


To calculate the straight-line distance between two postcodes you need the latitude and longitude of each one. There's an awful lot of waffle and smoke and mirrors out there on the internet about this. I could not make any of the ingenious solutions I found work. So I made my own, and it works a treat. What we can do now is print a list of distances from a given postcode to a list of other postcodes, sorted by increasing distance. I used postcodes of B&Q stores - easy to get from their own website and not likely to offend anyone.

The key to it all was understanding the Google Map API and realising that if I made an Ajax request to the map URL I would get back a whole bunch of useful stuff - including latitudes & longitudes. No PAFs. no Ordnance Survey, no lookup tables - just Google.

Acknowledgements. To save time, I used a couple of scripts which I found on the web:

  • To calculate the straight line distance between two points on a curved surface, I used a PHP class from here . It does lot's of other useful co-ordinate conversion as well.
  • To check that the postcode is well formed before submitting it to Google I used some Javascript from here. Bit of an overkill for my little demo. Well - actually not really needed, as the Google API sends back a very polite message if you submit a totally screwed up code. I still use it though, because some of the codes that look like rubbish to us turn out to be part of some other country's post code system !


Enter your own postcode where indicated, and click the "Show Distances" button. You'll get a list of postcodes and their locations sorted in order (top to bottom) with the closest to you at the top. (They are all the locations of B&Q DIY stores.)

From this data it's a very simple step to filter the list so that you only see postcodes within a certain distance from your own and from this they can be easily displayed on a Google Map.

Enter your postcode: