Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
238 lines (195 sloc) 10.7 KB
<!DOCTYPE html>
<html lang="en">
<title>Alternate routes with deviation constraints - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="IE=Edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample shows the usage of alternative routes, alternative types and supporting points with minimum deviation time and distance constraints." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, services, module, route, directions" />
<meta name="author" content="Microsoft Azure Maps" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link rel="stylesheet" href="" type="text/css" />
<script src=""></script>
<!-- Add a reference to the Azure Maps Services Module JavaScript file. -->
<script src=""></script>
<script type='text/javascript'>
var map, datasource, routeURL;
var start = [-122.33028, 47.60323];
var end = [-122.124, 47.67491];
//Colors for the different routes.
var routeColors = ['#2272B9', '#ff7b25', '#6b5b95', '#d64161', '#00cc66', '#000000'];
function GetMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-122.335, 47.608],
zoom: 15,
view: 'Auto',
//Add your Azure Maps subscription key to the map SDK. Get an Azure Maps key at
authOptions: {
authType: 'subscriptionKey',
subscriptionKey: '<Your Azure Maps Key>'
//Use SubscriptionKeyCredential with a subscription key
var subscriptionKeyCredential = new atlas.service.SubscriptionKeyCredential(atlas.getSubscriptionKey());
//Use subscriptionKeyCredential to create a pipeline
var pipeline = atlas.service.MapsURL.newPipeline(subscriptionKeyCredential);
//Construct the RouteURL object
routeURL = new atlas.service.RouteURL(pipeline);
//Wait until the map resources are ready.'ready', function () {
datasource = new atlas.source.DataSource();
//Create a layer for rendering the route line under the road labels.
map.layers.add(new atlas.layer.LineLayer(datasource, null, {
//Get the route color using the resultIndex property of the route line.
strokeColor: ['to-color', ['at', ['get', 'resultIndex'], ['literal', routeColors]]],
strokeWidth: 5,
lineJoin: 'round',
lineCap: 'round'
}), 'labels');
//Create a layer for rendering the start and end points of the route as symbols.
map.layers.add(new atlas.layer.SymbolLayer(datasource, null, {
textOptions: {
textField: ['get', 'title'],
offset: [0, 1.2]
filter: ['any', ['==', ['geometry-type'], 'Point'], ['==', ['geometry-type'], 'MultiPoint']] //Only render Point or MultiPoints in this layer.
function calculateRoute() {
//Create the GeoJSON objects which represent the start and end points of the route and add to data source.
new, { title: 'Start' }),
new, { title: 'End' })
//Get the coordnates of the start and end points.
var coordinates = [
//Get the route options from the form.
var options = {
maxAlternatives: parseInt(document.getElementById('alts').value),
minDeviationTime: parseInt(document.getElementById('mdt').value),
minDeviationDistance: parseInt(document.getElementById('mdd').value),
postBody: {
"supportingPoints": {
"type": "GeometryCollection",
"geometries": [
new[-122.27696, 47.589749]),//A supporitng point on the on I90.
if (options.maxAlternatives > 0) {
var altTypeElm = document.getElementById('alternativeType');
options.alternativeType = altTypeElm.options[altTypeElm.selectedIndex].value;
//Calculate a route.
routeURL.calculateRouteDirections(atlas.service.Aborter.timeout(10000), coordinates, options).then((directions) => {
//Get the route data as GeoJSON and add it to the data source.
var data = directions.geojson.getFeatures();
//Create a table with the route travel time/distance information.
var html = ['<table><tr><td>Route</td><td>Distance</td><td>Time</td></tr>'];
for (var i = 0; i < directions.routes.length; i++) {
var s = directions.routes[i].summary.travelTimeInSeconds % 60;
html.push('<tr><td><div class="colorBlock" style="background-color:',
routeColors[i], '"></div></td><td>',
//Convert distance to meters and round to 1 decimal place.
Math.round(directions.routes[i].summary.lengthInMeters / 1000 * 10) / 10, ' km</td><td>',
//Format time as min:sec. If seconds less than 10, prepend a 0 to the second value.
Math.round(directions.routes[i].summary.travelTimeInSeconds / 60), ':',
((s < 10) ? '0' : ''), s, ' min</td></tr>');
document.getElementById('resultsPanel').innerHTML = html.join('');
//Update the map view to center over the route.
bounds: data.bbox,
padding: 30 //Add a padding to account for the pixel size of symbols.
.sidePanel {
position: absolute;
top: 10px;
left: 10px;
border-radius: 10px;
background-color: white;
padding: 10px;
#resultsPanel {
position: absolute;
top: 10px;
right: 10px;
border-radius: 10px;
background-color: white;
padding: 10px;
#resultsPanel table {
border-collapse: collapse;
#resultsPanel td, #resultsPanel th {
border: 1px solid #ccc;
padding: 3px;
.colorBlock {
width: 30px;
height: 5px;
vertical-align: middle;
margin: auto;
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<div class="sidePanel">
<form oninput="alts.value=alternatives.value">
Alternatives: <output id="alts" for="alternatives">1</output>
<br />
<input id="alternatives" type="range" value="1" step="1" min="0" max="5" onchange="calculateRoute()"/>
Alternative Type:
<select id="alternativeType" onchange="calculateRoute()">
<option value="anyRoute" selected="selected">Any route</option>
<option value="betterRoute">Better route</option>
<br /><br />
<form oninput="mdt.value=minDevTime.value">
Min. Deviation Time(s): <output id="mdt" for="minDevTime">0</output>
<br />
<input id="minDevTime" type="range" value="0" step="1" min="0" max="1000" onchange="calculateRoute()"/>
<form oninput="mdd.value=minDevDistance.value">
Min. Deviation Distance(m): <output id="mdd" for="minDevDistance">0</output>
<br />
<input id="minDevDistance" type="range" value="0" step="100" min="0" max="5000" onchange="calculateRoute()"/>
<div id="resultsPanel"></div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend><h1 style="font-size:16px">Alternate routes with deviation constraints</h1></legend>
This sample shows the usage of alternative routes, <a href="" target="_blank">alternative types</a> and <a href="" target="_blank">supporting points</a> with minimum deviation time and distance constraints.
<br /><br />
To find alternatives to the main route simply change the number of alternatives in the form to see them on the map.
<br /><br />
For advanced alternative route calculation you can include supporting points along with min deviation time or distance constraints. When these constraints are used, the alternative routes will follow the reference route from the origin point for the given time or distance. In other words, the alternative routes just diverge from the referente route after the given constraints.
<br /><br />
Keep in mind that in order for these constraints to work you need to include in the supporting points the start and ending points of the desired route. If you just provide supporting points for the first 500 meters of the route, the min deviation constraints will only work for that section.
<br /><br />
<b>Alternative type</b>: By default the alternative type is 'Any route', which means that all the requested alternative routes will be returned. When 'Better route' is selected, the service will only return alternative routes that are better than the reference route.
You can’t perform that action at this time.