Skip to content

Commit cdf6c36

Browse files
authored
Merge pull request #183 from Azure-Samples/copilot/fix-180
Update "Route to a destination" sample to use new Azure Maps API version 2025-01-01
2 parents fd6a2e8 + edf0195 commit cdf6c36

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

Samples/Tutorials/Route/Route to a destination.html

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
var map, datasource;
2626

2727
// 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';
2929

3030
function getMap() {
3131
// Initialize a map instance.
@@ -85,13 +85,17 @@
8585
var startPosition = [-122.33028, 47.60323];
8686
var startPoint = new atlas.data.Feature(new atlas.data.Point(startPosition), {
8787
title: 'Seattle',
88-
iconImage: 'pin-blue'
88+
iconImage: 'pin-blue',
89+
pointIndex: 0,
90+
pointType: "waypoint"
8991
});
9092

9193
var endPosition = [-122.124, 47.67491];
9294
var endPoint = new atlas.data.Feature(new atlas.data.Point(endPosition), {
9395
title: 'Redmond',
94-
iconImage: 'pin-red'
96+
iconImage: 'pin-red',
97+
pointIndex: 1,
98+
pointType: "waypoint"
9599
});
96100

97101
// Add the data to the data source.
@@ -103,23 +107,20 @@
103107
padding: 50
104108
});
105109

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);
123124
});
124125
});
125126
}

0 commit comments

Comments
 (0)