You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
3. Next, add a reference to the Azure Maps Services module. This module is a JavaScript library that wraps the Azure Maps REST services, making them easy to use in JavaScript. The Services module is useful for powering search functionality.
177
-
178
-
```HTML
179
-
<!-- Add a reference to the Azure Maps Services Module JavaScript file. -->
5. In the body of the document, add a `header` tag. Inside the `header` tag, add the logo and company name.
184
+
4. In the body of the document, add a `header` tag. Inside the `header` tag, add the logo and company name.
192
185
193
186
```HTML
194
187
<header>
@@ -197,7 +190,7 @@ To create the HTML:
197
190
</header>
198
191
```
199
192
200
-
6. Add a `main` tag and create a search panel that has a text box and search button. Also, add `div` references for the map, the list panel, and the My Location GPS button.
193
+
5. Add a `main` tag and create a search panel that has a text box and search button. Also, add `div` references for the map, the list panel, and the My Location GPS button.
201
194
202
195
```HTML
203
196
<main>
@@ -461,7 +454,7 @@ To add the JavaScript:
461
454
var countrySet = ['US', 'CA', 'GB', 'FR','DE','IT','ES','NL','DK'];
462
455
463
456
//
464
-
var map, popup, datasource, iconLayer, centerMarker, searchURL;
457
+
var map, popup, datasource, iconLayer, centerMarker;
465
458
466
459
// Used in function updateListItems
467
460
var listItemTemplate ='<div class="listItem" onclick="itemSelected(\'{id}\')"><div class="listItem-title">{title}</div>{city}<br />Open until {closes}<br />{distance} miles away</div>';
@@ -491,12 +484,6 @@ To add the JavaScript:
491
484
//Create a pop-up window, but leave it closed so we can update it and display it later.
492
485
popup = new atlas.Popup();
493
486
494
-
//Use MapControlCredential to share authentication between a map control and the service module.
495
-
var pipeline = atlas.service.MapsURL.newPipeline(new atlas.service.MapControlCredential(map));
496
-
497
-
//Create an instance of the SearchURL client.
498
-
searchURL = new atlas.service.SearchURL(pipeline);
499
-
500
487
//If the user selects the search button, geocode the value the user passed in.
@@ -225,80 +222,92 @@ This section shows you how to use the Azure Maps Route service to get directions
225
222
>[!TIP]
226
223
>The Route service provides APIs to plan *fastest*, *shortest*, *eco*, or *thrilling* routes based on distance, traffic conditions, and mode of transport used. The service also lets users plan future routes based on historical traffic conditions. Users can see the prediction of route durations for any given time. For more information, see [Get Route directions API].
227
224
228
-
1. In the `GetMap` function, inside the control's `ready` event handler, add the following to the JavaScript code.
229
-
230
-
```JavaScript
231
-
//Use MapControlCredential to share authentication between a map control and the service module.
232
-
var pipeline = atlas.service.MapsURL.newPipeline(new atlas.service.MapControlCredential(map));
233
-
234
-
//Construct the RouteURL object
235
-
var routeURL = new atlas.service.RouteURL(pipeline);
236
-
```
237
-
238
-
* Use [MapControlCredential] to share authentication between a map control and the service module when creating a new [pipeline] object.
239
-
240
-
* The [routeURL] represents a URL to Azure Maps [Route service].
241
-
242
-
2. After setting up credentials and the URL, add the following JavaScript code to construct a truck route from the start to end points. This route is created and displayed for a truck carrying `USHazmatClass2` classed cargo.
225
+
1. In the `GetMap` function, inside the control's `ready` event handler, add the following JavaScript code to construct a truck route from the start to end points. This route is created and displayed for a truck carrying `USHazmatClass2` classed cargo.
243
226
244
227
```JavaScript
245
-
//Start and end point input to the routeURL
246
-
var coordinates= [[startPoint.geometry.coordinates[0], startPoint.geometry.coordinates[1]], [endPoint.geometry.coordinates[0], endPoint.geometry.coordinates[1]]];
247
-
228
+
//Start and end point input to the search route request
229
+
var query = startPoint.geometry.coordinates[1] + "," +
230
+
startPoint.geometry.coordinates[0] + ":" +
231
+
endPoint.geometry.coordinates[1] + "," +
232
+
endPoint.geometry.coordinates[0];
248
233
//Make a search route request for a truck vehicle type
//Get the route line and add some style properties to it.
260
-
var routeLine = data.features[0];
261
-
routeLine.properties.strokeColor = '#2272B9';
262
-
routeLine.properties.strokeWidth = 9;
263
-
264
-
//Add the route line to the data source. This should render below the car route which will likely be added to the data source faster, so insert it at index 0.
265
-
datasource.add(routeLine, 0);
234
+
var truckRouteUrl = `https://atlas.microsoft.com/route/directions/json?api-version=1.0&travelMode=truck&vehicleWidth=2&vehicleHeight=2&vehicleLength=5&vehicleLoadType=USHazmatClass2&query=${query}`;
235
+
fetch(truckRouteUrl, {
236
+
headers: {
237
+
"Subscription-Key": map.authentication.getToken()
238
+
}
239
+
})
240
+
.then((response) => response.json())
241
+
.then((response) => {
242
+
var route = response.routes[0];
243
+
//Create an array to store the coordinates of each turn
//Add the route line to the data source. We want this to render below the car route which will likely be added to the data source faster, so insert it at index 0.
254
+
datasource.add(
255
+
new atlas.data.Feature(new atlas.data.LineString(routeCoordinates), {
256
+
strokeColor: "#2272B9",
257
+
strokeWidth: 9
258
+
}),
259
+
0
260
+
);
266
261
});
267
262
```
268
263
269
264
About the above JavaScript:
270
265
271
266
* This code queries the Azure Maps Route service through the [Azure Maps Route Directions API].
272
-
* The route line is then extracted from the GeoJSON feature collection from the response that is extracted using the `geojson.getFeatures()` method.
267
+
* The route line is then created from the coordinates of each turn from the response.
273
268
* The route line is then added to the data source.
274
269
* Two properties are added to the truck route line: a blue stroke color `#2272B9`, and a stroke width of nine pixels.
275
270
* The route line is given an index of 0 to ensure that the truck route is rendered before any other lines in the data source. The reason is the truck route calculation are often slower than a car route calculation. If the truck route line is added to the data source after the car route, it will render above it.
276
271
277
272
>[!TIP]
278
273
> To see all possible options and values for the Azure Maps Route Directions API, see [URI Parameters for Post Route Directions].
279
274
280
-
3. Next, append the following JavaScript code to create a route for a car.
275
+
2. Next, append the following JavaScript code to create a route for a car.
//Add the route line to the data source. This will add the car route after the truck route.
294
-
datasource.add(routeLine);
297
+
//Add the route line to the data source. This will add the car route after the truck route.
298
+
datasource.add(
299
+
new atlas.data.Feature(new atlas.data.LineString(routeCoordinates), {
300
+
strokeColor: "#B76DAB",
301
+
strokeWidth: 5
302
+
})
303
+
);
295
304
});
296
305
```
297
306
298
307
About the above JavaScript:
299
308
300
309
* This code queries the Azure Maps routing service through the [Azure Maps Route Directions API] method.
301
-
* The route line is then extracted from the GeoJSON feature collection from the response that is extracted using the `geojson.getFeatures()` method then is added to the data source.
310
+
* The route line is then created from the coordinates of each turn and added to the data source.
302
311
* Two properties are added to the truck route line: a purple stroke color `#B76DAB`, and a stroke width of five pixels.
303
312
304
313
4. Save the **TruckRoute.html** file and refresh your web browser. The map should now display both the truck and car routes.
@@ -325,8 +334,6 @@ The next tutorial demonstrates the process of creating a simple store locator us
* This code constructs the route from the start to end point.
235
-
* The `routeURL` requests the Azure Maps Route service API to calculate route directions.
236
-
* A GeoJSON feature collection from the response is then extracted using the `geojson.getFeatures()` method and added to the data source.
236
+
* The `url` queries the Azure Maps Route service API to calculate route directions.
237
+
* An array of coordinates is then extracted from the response and added to the data source.
237
238
238
-
3. Save the **MapRoute.html** file and refresh your web browser. The map should now display the route from the start to end points.
239
+
2. Save the **MapRoute.html** file and refresh your web browser. The map should now display the route from the start to end points.
239
240
240
241
:::image type="content" source="./media/tutorial-route-location/map-route.png" lightbox="./media/tutorial-route-location/map-route.png" alt-text="A screenshot showing a map that demonstrates the Azure Map control and Route service.":::
241
242
@@ -260,12 +261,9 @@ The next tutorial shows you how to create a route query with restrictions, like
0 commit comments