# (8) - HERE Routing API - Matrix Routing What is Matrix Routing used for? If the main function of the Routing API is to calculate a route, whether it is from A to B or passing through a number of waypoints in the middle, we always calculate a route from the start point to the end point; but the function that Matrix Routing should provide is not only Therefore, its purpose is to calculate "one-to-many", "many-to-one", and "many-to-many" route calculations. * One-to-many: from one origination to multiple possible destinations. * Many-to-one: From multiple possible originations to one destination. * Many to many: from multiple possible originations to multiple possible destinations. In fact, in real life, we often encounter deterministic problems: Today, while riding a motorcycle on the road, I realized that the fuel tank was about to bottom out. Then I could only use my mobile phone to search for gas stations nearby, but if the map was spread out There are several gas stations nearby. How do I choose which gas station to go to? This can be regarded as a "one-to-many" scenario. It is necessary to calculate a route from a origination (my current location) to multiple possible destinations (all gas stations nearby), and choose the most favorable result. ![](https://i.imgur.com/f8Netei.jpg) Suppose today is a taxi company, and I receive a request from a customer to use an APP to call a taxi. How do I choose the one that will arrive at the customer's location as soon as possible from all the nearby taxis? This can be regarded as a "many-to-one" scenario. It is necessary to calculate multiple possible originations (a taxi that can carry passengers) to an end point (the location of the customer), and choose the most favorable result. ![](https://i.imgur.com/tbFNmMV.jpg) As for "many-to-many", it may be in the logistics fleet, and at the same time receiving many customer requests for pickup, it is necessary to calculate the positions of the fleet's vehicles and these customers, and calculate the most favorable result to meet the demand . The function of Matrix Routing of HERE Routing API is to output the "relative spatial relationship" between these start points and end points. You can use Matrix Routing to calculate the "cost coefficient" from point to point based on distance or time. The higher the cost, It means that the moving distance is longer or the time is longer; on the contrary, if the cost is lower, it means that the moving distance is shorter or the time is shorter. You can use this method to determine which result is the one you want most. The Matrix Routing function of HERE Routing API is similar to the basic route planning function, and also has the following features: * Support route planning for cars, trucks, locomotives, bicycles, public transportation and pedestrians. * Can be set to avoid specific roads or areas. * It can support real-time/historical traffic, estimate more accurate arrival time and avoid congested road sections. * You can choose time priority or distance priority. * Support multilingual. * Can avoid high-speed roads, toll roads, ferries, specific public transportation and so on. * Support the special restrictions of specific countries such as license plate restrictions, exhaust volume restrictions, low pollution areas, and specific road taxes. * Support detailed truck restriction definitions, such as length/width/height/weight/number of axles/cargo, etc. The output result of Matrix Routing is not a planned route, but a matrix or list, and the results from point to point are listed in sequence. For example, the following is the result of "two start points to two end points" calculated by Matrix Routing. You can see that the attribute "matrixEntry" contains a matrix, which are: 1. From "startIndex=0" to "destinationIndex=0", the cost coefficient is 875. 2. From "startIndex=0" to "destinationIndex=1", the cost coefficient is 1653. 3. From "startIndex=1" to "destinationIndex=0", the cost coefficient is 2686. 4. From "startIndex=1" to "destinationIndex=1", the cost coefficient is 2428. You might ask, where is the matrix? The matrix is here! With the results calculated by Matrix Routing, a matrix like this can be made, or a table, which is easier to understand. | | D0 | D1 | | ------ | -------- | -------- | | S0 | 875 | 1653 | | S1 | 2686 | 2428 | Therefore, we can know that the cost from "startIndex = 0" to "destinationIndex = 0" is the lowest, and the cost from "startIndex = 1" to "destinationIndex = 0" is the highest. ```json= { "response": { "metaInfo": { "timestamp": "2020-09-16T08:02:33Z", "mapVersion": "8.30.112.153", "moduleVersion": "7.2.202037-7814", "interfaceVersion": "2.6.76", "availableMapVersion": [ "8.30.112.153" ] }, "matrixEntry": [ { "startIndex": 0, "destinationIndex": 0, "summary": { "costFactor": 875 } }, { "startIndex": 0, "destinationIndex": 1, "summary": { "costFactor": 1653 } }, { "startIndex": 1, "destinationIndex": 0, "summary": { "costFactor": 2686 } }, { "startIndex": 1, "destinationIndex": 1, "summary": { "costFactor": 2428 } } ] } } ``` ## Use HERE Routing API Reference Client to play Matrix Routing In the article [Quickly Build Map Service (7)\- Know HERE Routing API-Route Planning](https://ithelp.ithome.com.tw/articles/10237145) We have already introduced [HERE Routing API Reference Client]( http://refclient.ext.here.com/) This useful tool, we can also use this tool to experience Matrix Routing, it will be more efficient. Open [HERE Routing API Reference Client](http://refclient.ext.here.com/), and then first press "New Tab +" in the upper left corner, select "Caculate Matrix", you will see more A "Matrix 1" tab page is created, this tab page is Matrix Routing; and the "OLS 1" next to it is Routing API V8. ![](https://i.imgur.com/vDSJWie.gif) Then we move the map to Taiwan. You can drag it directly on the map screen, or use the search function at the top of the screen. After entering a string, press "Enter" to move to the search result. ![](https://i.imgur.com/NPRGGjq.gif) You can see that the screen is already in Taipei City, and then we can press the right mouse button on the map to try it out. A menu will appear and you can perform some related operations. ![](https://i.imgur.com/4zLWEWZ.png) Then let's try the "one-to-many" route planning first. Please right-click anywhere on the map and select "Add Start" to add a origination, and right-click any other location to select "Add Destination" To add multiple endpoints. ![](https://i.imgur.com/h83ce1f.png) The "S0" on the map represents the origination, because we have only established one origination, so the serial number is 0; as for the destinations, there are five, namely "D0", "D1", "D2", "D3", and " D4". You can also manually enter the latitude and longitude coordinates of the origination and destination on the left side of the screen, and the origination and destination you added on the map will also be displayed on the left side. In the menu on the right, in "SEARCH RANGE" you can enter the maximum distance you want to query. If the destination exceeds this distance, it will not be included in the calculation. The "Mode" below can select the vehicle type, and the "ROUTE TYPE" is to select "fastest / fastest route" or "shortest / shortest route", and "TRAFFIC ROUTING" can choose whether to use real-time traffic for calculation. And the "Features" below can choose to avoid specific types of roads. You can also choose the departure time and avoid specific roads or areas and so on. These options are the same as the calculation routes we talked about before. ![](https://i.imgur.com/SZ82z1i.png) When we press "Send" in the lower right corner, it will be calculated and displayed on the screen. ![](https://i.imgur.com/Y2jHM0l.png) You can also right-click on the dotted line between the dots on the screen to select "Calc Route in New Tab", which will calculate the actual moving route. ![](https://i.imgur.com/EdG86BQ.gif) We can query the calculation result of each start point to the end point in the "MATRIX ENTRY" on the right side of the screen. For example, in this case, the travel time from "S0" to "D0" is 5 minutes and 43 seconds, the travel distance is 2.411 kilometers, and the cost is The coefficient is 355. And so on. In the final result, the lowest cost is to move from "S0" to "D4", so you can imagine that "S0" may be your current location, and "D4" may be the nearest of these gas stations. One. ``` Matrix Entry S0-D0 TRAVEL TIME: 05 min 43 sec DISTANCE: 2.144 km (1.332 mi) COST FACTOR: 355 Matrix Entry S0-D1 TRAVEL TIME: 05 min 51 sec DISTANCE: 1.831 km (1.138 mi) COST FACTOR: 380 Matrix Entry S0-D2 TRAVEL TIME: 06 min 04 sec DISTANCE: 2.166 km (1.346 mi) COST FACTOR: 369 Matrix Entry S0-D3 TRAVEL TIME: 07 min 12 sec DISTANCE: 2.459 km (1.528 mi) COST FACTOR: 448 Matrix Entry S0-D4 TRAVEL TIME: 04 min 46 sec DISTANCE: 1.833 km (1.139 mi) COST FACTOR: 291 ``` In addition to "one-to-many" calculations, you can also use [HERE Routing API Reference Client](http://refclient.ext.here.com/) to test "many-to-one" or "many-to-many" calculations. With the assistance of Matrix Routing, you can quickly understand the spatial relationship between point-to-point and assist you in making decisions about geographic location or map applications. Of course, you can also say that I can use the coordinates of two points to calculate a straight line. Why use Matrix Routing? In fact, in practice, it is true that straight-line distance is often used as the basis for calculation, but this will encounter certain problems: 1. The straight-line distance is short, but the road required for actual movement may be congested. 2. The straight-line distance is short, but there may be no road connection in the middle. 3. The straight-line distance is short, but road restrictions make it impossible to pass and must detour. For example, in the case we just calculated, although the straight-line distance from "S0" to "D1" appears to be the shortest at first glance, because of several turns in the middle, although the driving distance is only 1.831 kilometers, the turning and waiting for the traffic light Time is also taken into account, so the estimated time of arrival is 5 minutes and 51 seconds. On the other hand, from "S0" to "D4", although the straight-line distance is much longer than that of "S0" to "D1", because the main road passing through is relatively fast, the estimated time of arrival is only 4 minutes and 46 seconds, which is enough. It takes a full minute, so the cost is relatively low. ![](https://i.imgur.com/vItVtOn.png) The above result is the result of not considering the real-time traffic conditions. If we set "TRAFFIC ROUTING" to "Enabled" and run it again, let's try to see if the results are different. Here are the results: ``` Matrix Entry S0-D0 TRAVEL TIME: 07 min 27 sec DISTANCE: 2.144 km (1.332 mi) COST FACTOR: 459 Matrix Entry S0-D1 TRAVEL TIME: 08 min 30 sec DISTANCE: 2.017 km (1.253 mi) COST FACTOR: 544 Matrix Entry S0-D2 TRAVEL TIME: 11 min 02 sec DISTANCE: 2.184 km (1.357 mi) COST FACTOR: 703 Matrix Entry S0-D3 TRAVEL TIME: 10 min 03 sec DISTANCE: 2.640 km (1.640 mi) COST FACTOR: 636 Matrix Entry S0-D4 TRAVEL TIME: 06 min 03 sec DISTANCE: 1.833 km (1.139 mi) COST FACTOR: 368 ``` It can be seen from the above example that even if the real-time traffic condition calculation is added, the result has not changed, but the moving time is longer than when there is no real-time traffic condition. For example, the combination of "S0" to "D4" is still the fastest, but the moving time is stretched from 4 minutes 46 seconds to 6 minutes 03 seconds, which is faster than the 8 minutes 30 seconds of "S0" to "D1" Nearly one and a half minutes. Therefore, what I want to express here is that the straight-line distance is indeed simple and convenient, but the possible risks still need to be considered; however, the Matrix Routing function provided by the HERE Routing API, in addition to the traffic and vehicle types, are included in the calculation. , It is more able to calculate more accurate results in combination with real-time traffic conditions. Another situation you will encounter is as follows. This is a "many-to-one" scene. "D0" is the Carrefour Neihu store, and "S0", "S1" and "S2" represent three taxis respectively. This is a simulation A passenger calls for a taxi at the Carrefour Neihu store, and there are three taxis nearby in the dispatch system that can carry passengers, and one of the vehicles that arrives at the passenger's position must be selected the fastest. ![](https://i.imgur.com/hljP9zv.png) In terms of straight-line distance, "S0" is the closest to "D0" and "S1" is the farthest, but the actual calculation result is the fastest from "S1" to "D0" (only 3 minutes and 48 seconds) ), the travel distance is also the shortest (1.027 km). ``` Matrix Entry S0-D0 TRAVEL TIME: 09 min 20 sec DISTANCE: 3.088 km (1.919 mi) COST FACTOR: 621 Matrix Entry S1-D0 TRAVEL TIME: 03 min 48 sec DISTANCE: 1.027 km (0.638 mi) COST FACTOR: 277 Matrix Entry S2-D0 TRAVEL TIME: 06 min 12 sec DISTANCE: 2.321 km (1.442 mi) COST FACTOR: 395 ``` The key is that there is a river between "S0" and "D0", and you must detour to the Minquan Bridge before crossing the river to reach "D0": ![](https://i.imgur.com/HPrDsG8.png) Comparing the actual moving routes of "S1" and "S2", you will find that because the initial position of "S2" is in the lane and there is no main road connection between "S2" and "D0", it takes a detour, which cost 6 It takes only 3 minutes and 48 seconds to reach "D0" in minutes and 12 seconds instead of "S1". ![](https://i.imgur.com/ZSNQW72.png) Similarly, we can also copy the URL in the "API CALL" text box below: ```javascript= https://matrix.route.api.here.com/routing/7.2/calculatematrix.json ?mode=fastest;car;traffic:enabled &matrixattributes=ix,su &summaryattributes=cf,tt,di &start0=25.058879,121.568246 &start1=25.065293,121.580477 &start2=25.054913,121.580735 &destination0=25.060074,121.574855 &app_code=pxIXqdtgOSwQDXSDfjLQpw &app_id=cgZPrYfgRePXzXC3PbBp ``` Like the routing API’s route planning function, we need to modify the URL slightly before it can be used in our application or web page. The modified version is as follows: ```javascript= https://route.ls.hereapi.com/routing/7.2/calculatematrix.json ?mode=fastest;car;traffic:enabled // Calculate the fastest car route and consider real-time traffic &matrixattributes=ix,su // return index number (ix) and summary (su) in the result &summaryattributes=cf,tt,di // The cost coefficient (cf), travel time (tt) and travel distance (di) are listed in the summary &start0=25.058879,121.568246 // origination 0 &start1=25.065293,121.580477 // origination 1 &start2=25.054913,121.580735 // origination 2 &destination0=25.060074,121.574855 // end point 0 &apikey={APIKEY} // Your APIKEY ``` In this way, you should be more conceptual: ** What looks close is not necessarily near, and what looks far is not necessarily far; the cost of waiting is high, but the cost of waiting unexpectedly is higher! ** HERE's Matrix Routing can help you get the right spatial relationship and make the right decision.