|
30 | 30 | var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsToken'; |
31 | 31 |
|
32 | 32 | // URL for the Azure Maps Route API. |
33 | | - var routeUrl = 'https://{azMapsDomain}/route/directions/json?api-version=1.0&query={query}&routeRepresentation=polyline&travelMode=car&view=Auto'; |
| 33 | + var routeUrl = 'https://{azMapsDomain}/route/directions?api-version=2025-01-01'; |
34 | 34 |
|
35 | 35 | // Function to retrieve an Azure Maps access token. |
36 | 36 | function getToken(resolve, reject, map) { |
|
96 | 96 | var startPosition = [-122.33028, 47.60323]; |
97 | 97 | var startPoint = new atlas.data.Feature(new atlas.data.Point(startPosition), { |
98 | 98 | title: 'Seattle', |
99 | | - iconImage: 'pin-blue' |
| 99 | + iconImage: 'pin-blue', |
| 100 | + pointIndex: 0, |
| 101 | + pointType: "waypoint" |
100 | 102 | }); |
101 | 103 |
|
102 | 104 | var endPosition = [-122.124, 47.67491]; |
103 | 105 | var endPoint = new atlas.data.Feature(new atlas.data.Point(endPosition), { |
104 | 106 | title: 'Redmond', |
105 | | - iconImage: 'pin-red' |
| 107 | + iconImage: 'pin-red', |
| 108 | + pointIndex: 1, |
| 109 | + pointType: "waypoint" |
106 | 110 | }); |
107 | 111 |
|
108 | 112 | // Add the data to the data source. |
|
114 | 118 | padding: 50 |
115 | 119 | }); |
116 | 120 |
|
117 | | - // Create the route request with the query being the start and end point in the format 'startLongitude,startLatitude:endLongitude,endLatitude'. |
118 | | - var routeRequestURL = routeUrl |
119 | | - .replace('{query}', `${startPosition[1]},${startPosition[0]}:${endPosition[1]},${endPosition[0]}`); |
| 121 | + // Create the route request. |
| 122 | + var routeRequestBody = { |
| 123 | + type: 'FeatureCollection', |
| 124 | + features: [startPoint, endPoint], |
| 125 | + optimizeRoute: 'fastestWithTraffic', |
| 126 | + routeOutputOptions: ['routePath'], |
| 127 | + maxRouteCount: 1, |
| 128 | + travelMode: 'driving' |
| 129 | + }; |
120 | 130 |
|
121 | 131 | // Process the request and render the route result on the map. |
122 | | - processRequest(routeRequestURL).then(directions => { |
123 | | - // Extract the first route from the directions. |
124 | | - const route = directions.routes[0]; |
125 | | - |
126 | | - // Combine all leg coordinates into a single array. |
127 | | - const routeCoordinates = route.legs.flatMap(leg => leg.points.map(point => [point.longitude, point.latitude])); |
128 | | - |
129 | | - // Create a LineString from the route path points. |
130 | | - const routeLine = new atlas.data.LineString(routeCoordinates); |
131 | | - |
132 | | - // Add it to the data source. |
133 | | - datasource.add(routeLine); |
| 132 | + processPostRequest(routeUrl, JSON.stringify(routeRequestBody)).then(directions => { |
| 133 | + // Add directions to the data source. |
| 134 | + datasource.add(directions); |
134 | 135 | }); |
135 | 136 | }); |
136 | 137 | } |
|
0 commit comments