Presuming that you are using the Distance Matrix API (and not the Directions API) then the form of a request should be :
https:
And your code should look something like this :
https:
Obviously you need to replace "YOUR_API_KEY" with your API key from Google for which you need a billing account set up and
Maps Javascript API
enabled. The full set of optional parameters are described below. You will receive a response in JSON format (or XML if you replace
json?
with
xml?
that you need to parse. An example of some PHP code to parse a Distance Matrix API request is shown below. All the information you need can be found at
Distance Matrix API request and response | Google for Developers[
^].
<?php
$url = 'href="https://maps.googleapis.com/maps/api/distancematrix/outputFormat?parameters';
$jsonResponse = file_get_contents($url);
$data = json_decode($jsonResponse, true);
if ($data['status'] == 'OK') {
foreach ($data['rows'] as $row) {
foreach ($row['elements'] as $element) {
if ($element['status'] == 'OK') {
$distance = $element['distance']['text'];
$duration = $element['duration']['text'];
echo "Distance: $distance\n";
echo "Duration: $duration\n";
} else {
echo "Error: " . $element['status'] . "\n";
}
}
}
} else {
echo "Error: " . $data['status'] . "\n";
}
?>
Distance Matrix API Request :
https://maps.googleapis.com/maps/api/distancematrix/outputFormat?parameters
Where
outputFormat
may be either
json
or
xml
.
And
parameters
include :
origins
The starting point for calculating travel distance and time. You can supply one or more locations in the form of an address, latitude/longitude coordinates, or a place ID.
destinations
One or more locations to which to calculate travel distance and time.
mode
(optional)
Specifies the mode of transport. Options include driving, walking, bicycling, and transit.
units
(optional)
Specifies the unit system to use when expressing distance as text. Options include metric and imperial.
departure_time
(optional)
Specifies the desired time of departure as an integer in seconds since midnight, January 1, 1970 UTC.
traffic_model
(optional)
Specifies the assumptions to use when calculating time in traffic. Options include best_guess, pessimistic, and optimistic.
Mike