|
133 | 133 | }); |
134 | 134 | } |
135 | 135 |
|
136 | | - async function itineraryOptimization() { |
| 136 | + // Itinerary Optimization function |
| 137 | + async function itineraryOptimizationClicked() { |
137 | 138 |
|
138 | | - // Show loading icon. |
139 | | - document.getElementById('loadingIcon').style.display = ''; |
| 139 | + // Show loading icon |
| 140 | + showLoadingIcon(true); |
140 | 141 |
|
141 | 142 | // We need all coordinates together depots + stops |
142 | | - var allCoordinates = depots.concat(stops); |
| 143 | + const allCoordinates = [...depots, ...stops]; |
143 | 144 |
|
| 145 | + // Get the matrix for all coordinates |
| 146 | + var matrix = await getMatrixAsync(allCoordinates); |
| 147 | + |
| 148 | + // We use the NVIDIA cuOpt optimizer service to calculate the Multi-Itinerary Optimization. |
| 149 | + // more details: https://docs.nvidia.com/cuopt/user-guide/supported-features.html |
| 150 | + var solver = await getOptimizedRouteAsync(matrix); |
| 151 | + |
| 152 | + // Render the route on the map for each vehicle |
| 153 | + renderRoute(allCoordinates, solver); |
| 154 | + |
| 155 | + // Hide loading icon |
| 156 | + showLoadingIcon(false); |
| 157 | + } |
| 158 | + |
| 159 | + // Function to show/hide the loading icon |
| 160 | + function showLoadingIcon(visible) { |
| 161 | + document.getElementById('loadingIcon').style.display = visible ? '' : 'none'; |
| 162 | + }; |
| 163 | + |
| 164 | + // Function to get the route matrix for all coordinates |
| 165 | + async function getMatrixAsync(allCoordinates) { |
144 | 166 | // Create the request body to get a route matrix |
145 | 167 | var routeMatrixBody = { |
146 | 168 | origins: { |
|
156 | 178 | // Initiate an async route matrix request. |
157 | 179 | var responseRouteMatrix = await processPostRequest(routeMatrixAsyncUrl, JSON.stringify(routeMatrixBody)); |
158 | 180 |
|
159 | | - var matrix = responseRouteMatrix.matrix; |
160 | | - |
161 | | - // We use the NVIDIA optimizer service to calculate the Multi-Itinerary Optimization. |
162 | | - // more details: https://docs.nvidia.com/cuopt/user-guide/supported-features.html |
| 181 | + return responseRouteMatrix.matrix; |
| 182 | + } |
163 | 183 |
|
| 184 | + // Function to get the optimized routes |
| 185 | + async function getOptimizedRouteAsync(matrix) { |
164 | 186 | // Create the request body for the Multi-Itinerary Optimization. |
165 | 187 | var optimizerBody = { |
166 | 188 | requestBody: { |
|
205 | 227 | var optimizerUrl = 'https://api.nvcf.nvidia.com/v2/nvcf/exec/functions/4bb2668d-c877-4b89-aabf-1630ed9b32c3/versions/471b94dc-931e-4aa1-99f4-935f5f352737'; |
206 | 228 |
|
207 | 229 | // Bearer token for the NVIDIA optimizer service. |
208 | | - var token = ''; |
| 230 | + var tokenResponse = await fetch('https://samples.azuremaps.com/api/GetMIOKey'); |
| 231 | + var token = await tokenResponse.text(); |
209 | 232 |
|
210 | 233 | // Initiate an request to the NVIDIA optimizer service. |
211 | 234 | var optimizerResponse = await fetch(optimizerUrl, { |
|
251 | 274 | data = statusResponse.json(); |
252 | 275 | } |
253 | 276 |
|
254 | | - // Render the route on the map for each vehicle. |
255 | | - for (var vehicle in data.response.response.solver_response.vehicle_data) { |
| 277 | + return data.response.response.solver_response; |
| 278 | + } |
| 279 | + |
| 280 | + // Function to render the route on the map for each vehicle |
| 281 | + function renderRoute(allCoordinates, solver) { |
| 282 | + for (var vehicle in solver.vehicle_data) { |
256 | 283 |
|
257 | 284 | // Get the vehicle data. |
258 | | - var car = data.response.response.solver_response.vehicle_data[vehicle]; |
| 285 | + var car = solver.vehicle_data[vehicle]; |
259 | 286 |
|
260 | 287 | // Create a string of coordinates for the route. |
261 | 288 | var coordinateString = ""; |
|
286 | 313 | routeSource.add(routeLine); |
287 | 314 | }); |
288 | 315 | }; |
289 | | - |
290 | | - // Hide loading icon. |
291 | | - document.getElementById('loadingIcon').style.display = 'none'; |
292 | 316 | } |
293 | 317 | </script> |
294 | 318 | </head> |
295 | 319 | <body onload="getMap()"> |
296 | 320 | <div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div> |
297 | 321 |
|
298 | 322 | <div style="position:absolute;top:10px;left:10px;background-color:#fff;padding:10px;border-radius:5px;"> |
299 | | - <input type="button" value="Multi-Itinerary Optimization" onclick="itineraryOptimization()" /> |
| 323 | + <input type="button" value="Multi-Itinerary Optimization" onclick="itineraryOptimizationClicked()" /> |
300 | 324 | </div> |
301 | 325 |
|
302 | 326 | <img id="loadingIcon" src="/images/loadingIcon.gif" title="Loading" style="position:absolute;left:calc(50% - 25px);top:250px;display:none;" /> |
|
0 commit comments