|
21 | 21 | <script src="https://samples.azuremaps.com/lib/azure-maps/azure-maps-helper.min.js"></script> |
22 | 22 |
|
23 | 23 | <script> |
24 | | - var map, stopSource, depotSource, routeSource, stops, depots; |
| 24 | + var map, stopDataSource, depotDataSource, routeDataSource; |
| 25 | + var stops, depots, vehicles; |
| 26 | + |
| 27 | + // Colors for the different routes. |
| 28 | + var routeColors = ['#2272B9', '#ff7b25', '#6b5b95', '#d64161', '#00cc66', '#000000']; |
25 | 29 |
|
26 | 30 | // Your Azure Maps client id for accessing your Azure Maps account. |
27 | 31 | var azureMapsClientId = 'e6b6ab59-eb5d-4d25-aa57-581135b927f0'; |
|
50 | 54 | map = new atlas.Map('myMap', { |
51 | 55 | center: [-105.076, 40.56], |
52 | 56 | zoom: 11, |
53 | | - style: 'grayscale_dark', |
| 57 | + style: 'grayscale_light', |
54 | 58 | view: 'Auto', |
55 | 59 |
|
56 | 60 | // Add authentication details for connecting to Azure Maps. |
|
77 | 81 | // Download stop and depot location GeoJSON data. |
78 | 82 | Promise.all([ |
79 | 83 | fetch('/data/geojson/mio_stops.json').then(response => response.json()), |
80 | | - fetch('/data/geojson/mio_depots.json').then(response => response.json()) |
| 84 | + fetch('/data/geojson/mio_depots.json').then(response => response.json()), |
| 85 | + fetch('/data/geojson/mio_vehicles.json').then(response => response.json()) |
81 | 86 | ]).then(results => { |
82 | 87 | stops = results[0].features; |
83 | 88 | depots = results[1].features; |
| 89 | + vehicles = results[2]; |
84 | 90 |
|
85 | 91 | // Create a data source for stops. |
86 | | - stopSource = new atlas.source.DataSource(); |
87 | | - map.sources.add(stopSource); |
88 | | - stopSource.add(stops); |
| 92 | + stopDataSource = new atlas.source.DataSource(); |
| 93 | + map.sources.add(stopDataSource); |
| 94 | + stopDataSource.add(stops); |
89 | 95 |
|
90 | 96 | // Create a data source for depots. |
91 | | - depotSource = new atlas.source.DataSource(); |
92 | | - map.sources.add(depotSource); |
93 | | - depotSource.add(depots); |
| 97 | + depotDataSource = new atlas.source.DataSource(); |
| 98 | + map.sources.add(depotDataSource); |
| 99 | + depotDataSource.add(depots); |
94 | 100 |
|
95 | 101 | // Create a data source for routes. |
96 | | - routeSource = new atlas.source.DataSource(); |
97 | | - map.sources.add(routeSource); |
| 102 | + routeDataSource = new atlas.source.DataSource(); |
| 103 | + map.sources.add(routeDataSource); |
98 | 104 |
|
99 | 105 | // Add rendering layers below labels. |
100 | 106 | map.layers.add([ |
101 | | - new atlas.layer.LineLayer(routeSource, null, { |
102 | | - strokeColor: '#B97222', |
| 107 | + new atlas.layer.LineLayer(routeDataSource, null, { |
| 108 | + strokeColor: ['get', 'strokeColor'], |
103 | 109 | strokeWidth: 5, |
| 110 | + strokeOpacity: 0.8, |
104 | 111 | lineJoin: 'round', |
105 | 112 | lineCap: 'round' |
106 | 113 | }), |
107 | | - new atlas.layer.BubbleLayer(stopSource, null, { |
| 114 | + new atlas.layer.BubbleLayer(stopDataSource, null, { |
108 | 115 | createIndicators: true, // to enable bubble layer a11y feature |
109 | 116 | radius: 4, |
110 | 117 | strokeWidth: 0 |
111 | 118 | }) |
112 | 119 | ], 'labels'); |
113 | 120 |
|
114 | 121 | // Render an icon for each depot. |
115 | | - map.layers.add(new atlas.layer.SymbolLayer(depotSource, null, { |
| 122 | + map.layers.add(new atlas.layer.SymbolLayer(depotDataSource, null, { |
116 | 123 | iconOptions: { |
117 | 124 | image: 'fire-shield-icon', |
118 | 125 | size: 0.5, |
|
143 | 150 | const allCoordinates = [...depots, ...stops]; |
144 | 151 |
|
145 | 152 | // Get the matrix for all coordinates |
146 | | - var matrix = await getMatrixAsync(allCoordinates); |
| 153 | + const matrix = await getMatrixAsync(allCoordinates); |
147 | 154 |
|
148 | 155 | // We use the NVIDIA cuOpt optimizer service to calculate the Multi-Itinerary Optimization. |
149 | 156 | // more details: https://docs.nvidia.com/cuopt/user-guide/supported-features.html |
150 | | - var solver = await getOptimizedRouteAsync(matrix); |
| 157 | + const solver = await getOptimizedRouteAsync(matrix); |
151 | 158 |
|
152 | 159 | // Render the route on the map for each vehicle |
153 | 160 | renderRoute(allCoordinates, solver); |
|
175 | 182 | } |
176 | 183 | }; |
177 | 184 |
|
178 | | - // Initiate an async route matrix request. |
| 185 | + // Initiate an async route matrix request |
179 | 186 | var responseRouteMatrix = await processPostRequest(routeMatrixAsyncUrl, JSON.stringify(routeMatrixBody)); |
180 | 187 |
|
181 | 188 | return responseRouteMatrix.matrix; |
182 | 189 | } |
183 | 190 |
|
184 | 191 | // Function to get the optimized routes |
185 | 192 | async function getOptimizedRouteAsync(matrix) { |
186 | | - // Create the request body for the Multi-Itinerary Optimization. |
| 193 | + |
| 194 | + // Function to convert hours to seconds |
| 195 | + const convertHoursToSeconds = (hours) => hours * 60 * 60; |
| 196 | + |
| 197 | + const stopslength = stops.length; |
| 198 | + const depotslength = depots.length; |
| 199 | + |
| 200 | + // Create the request body for the Multi-Itinerary Optimization |
187 | 201 | var optimizerBody = { |
188 | 202 | requestBody: { |
189 | 203 | data: { |
190 | 204 | cost_matrix_data: |
191 | 205 | { |
192 | 206 | data: |
193 | 207 | { |
| 208 | + // "1" = is the id for the vehicle_type_id |
194 | 209 | "1": matrix.map(row => row.map(item => item.response.routeSummary.lengthInMeters)) |
195 | 210 | } |
196 | 211 | }, |
197 | 212 | travel_time_matrix_data: |
198 | 213 | { |
199 | 214 | data: |
200 | 215 | { |
| 216 | + // "1" = is the id for the vehicle_type_id |
201 | 217 | "1": matrix.map(row => row.map(item => item.response.routeSummary.travelTimeInSeconds)) |
202 | 218 | } |
203 | 219 | }, |
204 | 220 | fleet_data: { |
205 | 221 | // in this sample we have 4 vehicles, |
206 | 222 | // 2 vehicles for each depot (we have 2 depots) |
207 | 223 | vehicle_locations: [[0, 0], [0, 0], [1, 1], [1, 1]], // [start location in array Depot, stop location in array Depot] |
208 | | - vehicle_ids: ["CarA", "CarB", "CarC", "CarD"], // the id for each vehicle |
209 | | - vehicle_types: [1, 1, 1, 1], // cost_matrix_data & travel_time_matrix_data data id "1" |
210 | | - capacities: [[75, 75, 75, 75]], // the capacity for each vehicle |
| 224 | + vehicle_ids: vehicles.map(vehicle => vehicle.name), // ["CarA", "CarB", "CarC", "CarD"], // the id for each vehicle |
| 225 | + vehicle_types: vehicles.map(vehicle => vehicle.vehicle_type_id), // cost_matrix_data & travel_time_matrix_data data id "1" |
| 226 | + capacities: [vehicles.map(vehicle => vehicle.capacity)], // the capacity for each vehicle |
211 | 227 | //vehicle_time_windows: [[8, 17], [8, 17], [8, 17], [10, 20], [10, 20]], // the time in seconds |
212 | 228 | //vehicle_break_time_windows: [[[12, 14], [12, 14], [12, 14], [12, 14], [12, 14]]], // the time in seconds |
213 | | - //vehicle_break_durations: [[0, 0, 0, 0, 0]] // the time in seconds |
| 229 | + //vehicle_break_durations: [vehicles.map(vehicle => convertHoursToSeconds(vehicle.break_duration))] // the time in seconds |
214 | 230 | }, |
215 | 231 | task_data: { |
216 | | - task_locations: [2, 3, 4, 5, 6, 7, 8, 9], // the index in array Stops. the first 2 stops are the depots and does not count as tasks |
217 | | - demand: [[30, 40, 40, 30, 30, 40, 40, 30]], // the demand for each task/stop |
218 | | - //task_time_windows: [[8, 17], [8, 17], [8, 17], [17, 20]], // the time in seconds |
219 | | - //service_times: [0, 0, 0, 0] // the time in seconds |
| 232 | + task_locations: Array.from({ length: stopslength }, (_, index) => depotslength + index), // the index in array Stops. the first 2 stops are the depots and does not count as tasks |
| 233 | + demand: [stops.map(stop => stop.properties.demand)], // the demand for each task/stop |
| 234 | + // task_time_windows: [stops.map(stop => { |
| 235 | + // const openingHours = stop.properties.opening_hours; |
| 236 | + // return openingHours.map(convertHoursToSeconds); |
| 237 | + // })], // the time in seconds |
| 238 | + service_times: stops.map(stop => convertHoursToSeconds(stop.properties.service_time)) // the time in seconds |
220 | 239 | } |
221 | 240 | }, |
222 | 241 | client_version: "custom" |
|
279 | 298 |
|
280 | 299 | // Function to render the route on the map for each vehicle |
281 | 300 | function renderRoute(allCoordinates, solver) { |
| 301 | + var index = 0; |
282 | 302 | for (var vehicle in solver.vehicle_data) { |
283 | 303 |
|
284 | 304 | // Get the vehicle data. |
|
307 | 327 | const routeCoordinates = route.legs.flatMap(leg => leg.points.map(point => [point.longitude, point.latitude])); |
308 | 328 |
|
309 | 329 | // Create a LineString from the route path points. |
310 | | - const routeLine = new atlas.data.LineString(routeCoordinates); |
| 330 | + var routeLine = new atlas.data.LineString(routeCoordinates); |
311 | 331 |
|
312 | 332 | // Add it to the data source. |
313 | | - routeSource.add(routeLine); |
| 333 | + routeDataSource.add(new atlas.data.Feature(new atlas.data.LineString(routeCoordinates), { |
| 334 | + // Set the stroke color of the route based on the vehicle color. |
| 335 | + strokeColor: routeColors[index % routeColors.length] |
| 336 | + })); |
| 337 | + |
| 338 | + index++; |
314 | 339 | }); |
315 | 340 | }; |
316 | 341 | } |
|
0 commit comments