Hi,
I’m writing a system for a charity which requires us to cost journeys. I could sit in front of the computer, and type pairs of postcodes to get the information, but doing it this way is far more interesting.
Here is my code so far:
<?php
$endpoint = 'http://maps.google.com/maps/api/directions/json?';
$params = array(
"origin" => "SL8 5JU",
'destination' => "HP13 5RD",
'mode' => 'driving',
'sensor' => 'false',
);
$json = file_get_contents($endpoint . http_build_query($params));
$data = json_decode($json);
if ($data->status === 'OK') {
print_r($data);
$route = $data->routes[0];
// Get the distance.
$distance = $route->legs[0]->distance->text;
// Break the distance into value/unit variables
list($miles, $unit) = explode(' ', $distance);
// Remove any commas.
$miles = str_replace(',', '', $miles);
// Cast the value as a floating point number to do any comparisons.
if ((float)$miles > 2250) {
print 'TRUE';
}
}
I got an API key from Google maps, but not entirely sure what to do with it.
Thanks.