What Is Distance Matrix and How to Get Started
Distance matrix, also known as matrix routing, is the calculation of duration and distance among several origins and destinations. For example, in a ride-sharing application, distance matrix can identify the closest driver when a rider requests a ride. You can think of distance matrix as a more complex routing API. With a routing API, you can get the distance and duration between one origin and destination; with Matrix Routing, you can get distance and duration among several origins and destinations.
So a distance matrix for this task shows the distance and/or duration for the routes that each driver would have to take to pick up the rider.
[
distance( rider : driver 1),
distance( rider : driver 2),
distance( rider : driver 3),
...
]and/or[
duration( rider : driver 1),
duration( rider : driver 2),
duration( rider : driver 3),
...
]
Solving this problem is actually pretty difficult when it comes to large matrices when you want to account for things like traffic and routing commercial vehicles like trucks that have different road regulations than your average car.
For this example, I’ll use HERE’s Matrix Routing API:
Method URL: https://matrix.router.hereapi.com/v8/matrix?apiKey=API_KEY&async=false
HTTP method: POST
Accepted content types: application/json
Body:
{
"origins": [
{
"lat": 47.673993,
"lng": -122.356108
},
{
"lat": 47.656910,
"lng": -122.362823
},
{
"lat": 47.648015,
"lng": -122.335674
},
{
"lat": 47.653022,
"lng": -122.312461
},
{
"lat":47.675796,
"lng": -122.311520
}
],
"destinations": [
{
"lat": 47.661438,
"lng": -122.336427
}
],
"regionDefinition": {
"type": "world"
}
}
And the response from that call gives us the duration for each driver to get to the passenger taking traffic into account. The travelTimes array shows the duration to get from each origin to destination, and the closest driver is the one with the smallest travelTime: 375. Following the same order in the call we made, the corresponding is third one in the array and they are 375 seconds (~6 minutes) away from the rider.
{
"matrixId": "5acf58a7-c3d5-4389-9c50-d67833bc47b6",
"matrix": {t
"numOrigins": 5,
"numDestinations": 1,
"travelTimes": [
604,
630,
375,
517,
639
]
},
"regionDefinition": {
"type": "world"
}
}
With HERE Matrix Routing you can calculate matrices as large as 10,000 X 10,000. In an Nx1 matrix, the results show travel times as an array with the same size and order as the origins and each row represents the travel times between each origin and the destination. For NxM Matrices though, the results are in the order of each origin followed by all matched destinations. For example, the results for an Nx2 matrix would look like:
origin1-destination1
origin1-destination2
origin2-destination1
origin2-destination2
...
In this example, I used what’s called Flexible mode and it allows taking traffic into account and using locations from all over the world. There are two other modes that allow for larger matrix sizes, with the limitation of not taking traffic into account or having a more closely defined region.
For more, you can visit HERE Matrix Routing documentation. I hope you’ve enjoyed learning more about distance matrices!