When you have requested your distance matrix, the returned object is an DistanceMatrixResponse
. It wraps a status,
origins, destinations and rows.
The available status are defined by the DistanceMatrixStatus
constants.
$status = $response->getStatus();
It contains an array of addresses as returned by the API from your original request. These are formatted by the geocoder and localized according to the language parameter passed with the request.
$origins = $response->getOrigins();
It contains an array of addresses as returned by the API from your original request. As with distance matrix origins, these are localized if appropriate.
$destinations = $response->getDestinations();
When the Distance Matrix API returns results, it places them within a rows array. Even if no results are returned (such as when the origins and/or destinations don't exist), it still returns an empty array. Rows are ordered according to the values in the origin parameter of the request. Each row corresponds to an origin, and each element within that row corresponds to a pairing of the origin with a destination value.
Each row array contains one or more distance matrix element entries, which in turn contain the information about a single origin-destination pairing.
foreach ($response->getRows() as $row) {
// ...
}
The information about each origin-destination pairing is represented by the DistanceMatrixElement
. An element
contains a status, a duration and a distance.
foreach ($row->getElements() as $element) {
// ...
}
The available status are defined by the DistanceMatrixElementStatus
constants.
$status = $element->getStatus();
The duration of this route represented by Distance
.
$distance = $element->getDistance();
If you want to learn more about the duration, you can read its documentation.
The duration of this route represented by Duration
.
$duration = $element->getDuration();
If you want to learn more about the duration, you can read its documentation.