I am looking for a way to calculate approximate distance between two UK postcodes (distance in the straight line is good enough) for analysing data. Preferably easily accessible from java, but C#, native C++ etc. are fine as well.
2 Answers
First, you need to translate the postcode into useful coordinates. For example, the Easting and Northing values from a Postcode lookup table, like the one from here: http://www.doogal.co.uk/UKPostcodes.php
These Easting and Northing are UK Ordnance Survey grid coordinates in metres from the OS map origin.
Convert them into Kilometres by dividing by 1000
Then use a simple Pythagoros triangle formula. Say the two points have Easting and Northing values (in kilometres) of E1, N1 and E2, N2
Distance between them in Kilometres = Square root of ( abs(E1-E2)^2 + abs(N1-M2)^2 )

- 218
- 2
- 7
-
I believe this should be Square root of ( abs(E1-E2)^2 + abs(N1-N2)^2 ) – Ben Simpson May 23 '13 at 13:58
-
1A bit late to the party, but I must add that using Pythagoras for distance assumes a flat surface. The Earth is of course curved. So the father away the two GPS coordinates, the bigger the error. – Erwin Moller Jun 11 '18 at 10:17
The following question answers the exact same question only in PHP specifically:
Using PHP and google Maps Api to work out distance between 2 post codes (UK)
It leverages a web service API though, so you should be able to use any basic rest api to leverage it (I'm sure there are well documented options in Java, C# and C++).