|
25 | 25 | var map, datasource; |
26 | 26 |
|
27 | 27 | // URL for the Azure Maps Route API. |
28 | | - var routeUrl = 'https://{azMapsDomain}/route/directions/json?api-version=1.0&query={query}&routeRepresentation=polyline&travelMode=car&view=Auto'; |
| 28 | + var routeUrl = 'https://{azMapsDomain}/route/directions?api-version=2025-01-01'; |
29 | 29 |
|
30 | 30 | function getMap() { |
31 | 31 | // Initialize a map instance. |
|
85 | 85 | var startPosition = [-122.33028, 47.60323]; |
86 | 86 | var startPoint = new atlas.data.Feature(new atlas.data.Point(startPosition), { |
87 | 87 | title: 'Seattle', |
88 | | - iconImage: 'pin-blue' |
| 88 | + iconImage: 'pin-blue', |
| 89 | + pointIndex: 0, |
| 90 | + pointType: "waypoint" |
89 | 91 | }); |
90 | 92 |
|
91 | 93 | var endPosition = [-122.124, 47.67491]; |
92 | 94 | var endPoint = new atlas.data.Feature(new atlas.data.Point(endPosition), { |
93 | 95 | title: 'Redmond', |
94 | | - iconImage: 'pin-red' |
| 96 | + iconImage: 'pin-red', |
| 97 | + pointIndex: 1, |
| 98 | + pointType: "waypoint" |
95 | 99 | }); |
96 | 100 |
|
97 | 101 | // Add the data to the data source. |
|
103 | 107 | padding: 50 |
104 | 108 | }); |
105 | 109 |
|
106 | | - // Create the route request with the query being the start and end point in the format 'startLongitude,startLatitude:endLongitude,endLatitude'. |
107 | | - var routeRequestURL = routeUrl |
108 | | - .replace('{query}', `${startPosition[1]},${startPosition[0]}:${endPosition[1]},${endPosition[0]}`); |
109 | | - |
110 | | - // Process the request and render the route result on the map. |
111 | | - processRequest(routeRequestURL).then(directions => { |
112 | | - // Extract the first route from the directions. |
113 | | - const route = directions.routes[0]; |
114 | | - |
115 | | - // Combine all leg coordinates into a single array. |
116 | | - const routeCoordinates = route.legs.flatMap(leg => leg.points.map(point => [point.longitude, point.latitude])); |
117 | | - |
118 | | - // Create a LineString from the route path points. |
119 | | - const routeLine = new atlas.data.LineString(routeCoordinates); |
120 | | - |
121 | | - // Add it to the data source. |
122 | | - datasource.add(routeLine); |
| 110 | + // Create the route request body using GeoJSON FeatureCollection. |
| 111 | + var routeRequestBody = { |
| 112 | + type: 'FeatureCollection', |
| 113 | + features: [startPoint, endPoint], |
| 114 | + optimizeRoute: 'fastestWithTraffic', |
| 115 | + routeOutputOptions: ['routePath'], |
| 116 | + maxRouteCount: 1, |
| 117 | + travelMode: 'driving' |
| 118 | + }; |
| 119 | + |
| 120 | + // Process the POST request and render the route result on the map. |
| 121 | + processPostRequest(routeUrl, JSON.stringify(routeRequestBody)).then(directions => { |
| 122 | + // Add directions to the data source. |
| 123 | + datasource.add(directions); |
123 | 124 | }); |
124 | 125 | }); |
125 | 126 | } |
|
0 commit comments