19

Hay, I wondering how to work out distance between 2 post codes using PHP and Google Maps Api.

Any one got any idea or links to examples.

dotty
  • 40,405
  • 66
  • 150
  • 195

5 Answers5

31

Assuming you are looking for the geographic distance, first you need to get the latitude and longitude of your two postcodes with the Google Maps server-side geocoding services as in the following example:

$url = 'http://maps.google.com/maps/geo?q=EC3M,+UK&output=csv&sensor=false';

$data = @file_get_contents($url);

$result = explode(",", $data);

echo $result[0]; // status code
echo $result[1]; // accuracy
echo $result[2]; // latitude
echo $result[3]; // longitude

Then you can calculate the distance between the coordinates of your two postcodes by using a great-circle distance implementation such as the following:

Note that the server-side geocoding service may only be used in conjunction with displaying results on a Google map; geocoding results without displaying them on a map is prohibited by the Google Maps API Terms of Service License Restrictions.


UPDATE:

If you are looking for the driving distance instead of the geographical distance, note that there is no documented and approved method at the moment to access the Google Maps Directions API via an HTTP request on the server-side.

Nevertheless, an undocumented method that returns a JSON output is the following:

http://maps.google.com/maps/nav?q=from:London%20to:Dover

This will return you the driving directions, along with the total driving distance in JSON format: "meters":122977.

The format of the q parameter should be from:xxx%20to:yyy. Replace xxx and yyy with the start and destination respectively. You can use latitude and a longitude coordinates instead of full addresses:

http://maps.google.com/maps/nav?q=from:51.519894,-0.105667%20to:51.129079,1.306925

Note that not only this is undocumented, but it may also violate the restrictions 10.1 and 10.5 of the Google Maps API Terms and Conditions.

You may also be interesting in checking out the following related articles:

Community
  • 1
  • 1
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • 1
    This is a great explanation of how to do it. But be aware, since you are asking specifically about the UK, that the Google geolocation isn't that accurate for UK postcodes. It might be OK for your purposes though, depends what you need. – MarkJ Feb 20 '10 at 11:37
  • @Mark: Thanks for sharing this info on the accuracy of UK postcodes. – Daniel Vassallo Feb 20 '10 at 12:24
  • driving distance does appear to be officially documented here https://developers.google.com/maps/documentation/distance-matrix/start edit: actually i noticed they havent used a php example – Jevon May 06 '22 at 21:30
4

Don't know if the V3 API changes anything, but I've been using this for quite a while and it still works:

From and to are each expressed as latitude,longitude (an address would probably work; I'm sure I tried it but don't remember and I had the coordinates anyway)

$base_url = 'http://maps.googleapis.com/maps/api/directions/xml?sensor=false';
$xml = simplexml_load_file("$base_url&origin=$from&destination=$to");
$distance = (string)$xml->route->leg->distance->text;
$duration = (string)$xml->route->leg->duration->text
Cuse70
  • 271
  • 3
  • 5
2

Building on Daniel's answer, you can use Google's Geocoding API to get car, public transport, walking and cycling distances easily :

$postcode1='W1J0DB';
$postcode2='W23UW';
$result = array();

$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=bicycling&language=en-EN&sensor=false";

$data = @file_get_contents($url);

$result = json_decode($data, true);
print_r($result);

Be sure to replace &mode=bicycling with your preferred parameter:

  • &mode=driving
  • &mode=bicycling
  • &mode=transit
  • &mode=walking
Nao
  • 23
  • 2
1

This is how I got it to work in 2022:

$postcode1 = '78211';
$postcode2 = '78355';
$commute_mode = 'driving';
$api_key = 'YOUR-KEY-HERE';
$result = array();

$url = "https://maps.googleapis.com/maps/api/distancematrix/json?key=$api_key&origins=$postcode1&destinations=$postcode2&mode=$commute_mode&language=en-EN&sensor=false";

$data = @file_get_contents($url);

$result = json_decode($data, true);

echo '<pre>';
print_r($result);
echo '</pre>';

Make sure to get your Google Maps API and replace the text YOUR-KEY-HERE with your actual API Key and change the other parameters as you see fit. Google gives you free maploads every month upto a value of $200, so you can integrate Google Maps API easily and test the same to see if it fits your expectations.

Devner
  • 6,825
  • 11
  • 63
  • 104
0

The Google Maps API is in JavaScript, not PHP. To achive what you want, turn the 2 post codes to LatLang coordinates and use the function distanceFrom to find the distance between them.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
  • Is there anyway i can return JSON data? I going to be doing AJAX calls to work out driving distance between 2 places. – dotty Feb 19 '10 at 12:27
  • You should be able to. Data coming from the Google API is already in JSON form. Just sent it back to PHP, or parse it client-side using Javascript. – Traveling Tech Guy Feb 20 '10 at 09:45