views:

1970

answers:

3

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.

+4  A: 

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:

Daniel Vassallo
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
@Mark: Thanks for sharing this info on the accuracy of UK postcodes.
Daniel Vassallo
A: 

Take a look at the mailing list here:

http://groups.google.com/group/Google-Maps-API/browse_thread/thread/71e6aa2c3de66127

Once you decide if you want a straight line or driving distance you can calculate it really easily. You don't have to use PHP but there are examples.

Look at this also:

http://groups.google.com/group/google-maps-api/browse_thread/thread/d43de63d05adc62d/1a04053f39a3edf3?lnk=gst&q=calculate+distance#1a04053f39a3edf3

Laykes
A: 

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.

Take a look at this article for some sample code.

Traveling Tech Guy
Is there anyway i can return JSON data? I going to be doing AJAX calls to work out driving distance between 2 places.
dotty
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