Using Google Maps, Geocoding and PHP to find the distance between UK Postcodes

UPDATE: A new version which calculates distance based on roads is available.

If you’re looking to make any kind of radius checker, delivery calculator etc, you will need to have some method of calculating this distance. Unfortunately for us in the UK, Royal Mail keep a tight grip on postcode data.

As a result, the best low-budget way of finding postcodes is by using the Google Maps api – which in itself isn’t 100% accurate (but good enough).

So we can use the following code:

// Specify Postcodes to Geocode
$postcode1 = 'BH151DA';
$postcode2 = 'BH213AP';

// Geocode Postcodes & Get Co-ordinates 1st Postcode
$pc1 = ''.$postcode1.',+UK&output=csv&sensor=false';
$data1 = @file_get_contents($pc1);
$result1 = explode(",", $data1);
$custlat1 = $result1[2];
$custlong1 = $result1[3];

// Geocode Postcodes & Get Co-ordinates 2nd Postcode
$pc2 = ''.$postcode2.',+UK&output=csv&sensor=false';
$data2 = @file_get_contents($pc2);
$result2 = explode(",", $data2);
$custlat2 = $result2[2];
$custlong2 = $result2[3];

// Work out the distance!
$pi80 = M_PI / 180;
$custlat1 *= $pi80;
$custlong1 *= $pi80;
$custlat2 *= $pi80;
$custlong2 *= $pi80;

$r = 6372.797; // mean radius of Earth in km
$dlat = $custlat2 - $custlat1;
$dlng = $custlong2 - $custlong1;
$a = sin($dlat / 2) * sin($dlat / 2) + cos($custlat1) * cos($custlat2) * sin($dlng / 2) * sin($dlng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));

// Distance in KM
$km = round($r * $c, 2);

// Distance in Miles
$miles = round($km * 0.621371192, 2);

echo 'The distance between '.$postcode1.' and '.$postcode2.' is '.$km.'Km ('.$miles.' miles).';


You could use $result1[0] and $result2[0] to check codes. If the value is anything other than 200 the postcode is invalid. Also note UK is also searched for to guarantee correct results!

The result is also rounded to make sure we only have 2 decimal places. Make sure your postcodes do not have any spaces in when they go to Google, if you’re collecting them from a form maybe use:

function nowhitespace($data) {
return preg_replace('/\s/', '', $data);
$postcode1 = nowhitespace($postcode1);

to remove all spaces before processing, and the following to check it’s ok after processing:

if (($result1[0] != 200) || ($result2[0] != 200)) {
echo "<p>Invalid Postcode(s) Entered. Please try again.</p>";
} else {

Good luck!


Tricky Life on 2 October 2010 at 3:37 pm says:

thnx for the tutorial. Let me try it :-)
One thing, does it give the bird’s eye distance, or is it the distance by road??
thnx anyway :)

dave on 3 October 2010 at 10:10 pm says:

Birds eye – you can do distance by road but I don’t think it’s permitted by the Google Maps Api TOS…so probably best not to :)

Tim on 6 November 2010 at 5:30 pm says:

Awesome script, however it seems that $lat1 and $lat2 aren’t ever defined. Are those actually needed? (line 30)

dave on 15 November 2010 at 9:41 pm says:

Yes, an error on my part (now updated).

Thanks for that!

Darryl on 8 December 2010 at 3:52 pm says:

This could be exactly what i’m after. the problem is you’ve written it in PHP and I dont understand some of the code. Is there a way (or explanation) I could transfer it into ASP?


dave on 8 December 2010 at 4:46 pm says:

Hi Darryl,

Sorry I have no experience with ASP myself but if you’ve got questions about individual parts I’ll explain them further?

Darryl on 8 December 2010 at 5:38 pm says:

I figured it out. but the distance is out by ~.25miles. Is this the fault of the googlemaps data or is it the formula?

pandurang on 6 April 2011 at 10:14 am says:

Thanks a lot you have save my time

NortDevil on 19 April 2011 at 9:14 am says:

Lovely, much thankness :) This was exactly what I needed to check distance from postcode and works like a charm.

Turkey on 9 October 2011 at 4:50 pm says:

Finally i found article about google maps and php distance calculater. Thank you,

David on 18 May 2012 at 2:45 pm says:

I have made sense of your code and looks really good so thank you. However regardless of what postcode I put I get the post code is not valid error! Any help would be brilliant thanks.

dave on 18 May 2012 at 10:26 pm says:

I’ve just tested it:

And it appears to work. Do you have problems if you just use the code as shown, or have you made modifications?