Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
142 lines (119 sloc) 6.63 KB
<!DOCTYPE html>
<html lang="en">
<head>
<title>Snap drawn line to roads - 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 how to snap a line drawn using the draiwng tools to the road network. " />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, services, module, route, directions, snap to roads, snapping, gps traces, gps trace, snap to road" />
<meta name="author" content="Microsoft Azure Maps" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link rel="stylesheet" href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.css" type="text/css" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.js"></script>
<!-- Add references to the Azure Maps Map Drawing Tools JavaScript and CSS files. -->
<link rel="stylesheet" href="https://atlas.microsoft.com/sdk/javascript/drawing/0.1/atlas-drawing.min.css" type="text/css" />
<script src="https://atlas.microsoft.com/sdk/javascript/drawing/0.1/atlas-drawing.min.js"></script>
<!-- Add a reference to the Azure Maps Services Module JavaScript file. -->
<script src="https://atlas.microsoft.com/sdk/javascript/service/2/atlas-service.min.js"></script>
<script type='text/javascript'>
var map, datasource, routeURL, drawingManager;
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 https://azure.com/maps
authOptions: {
authType: 'subscriptionKey',
subscriptionKey: '<Your Azure Maps Key>'
}
});
//Use subscriptionKeyCredential to create a pipeline
var pipeline = atlas.service.MapsURL.newPipeline(new atlas.service.SubscriptionKeyCredential(atlas.getSubscriptionKey()));
//Construct the RouteURL object
routeURL = new atlas.service.RouteURL(pipeline);
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a data source for the route line.
datasource = new atlas.source.DataSource();
map.sources.add(datasource);
//Create a layer for rendering the route line under the road labels.
map.layers.add(new atlas.layer.LineLayer(datasource, null, {
strokeWidth: 5,
lineJoin: 'round',
lineCap: 'round'
}), 'labels');
//Create an instance of the drawing manager and display the line drawing option of the drawing toolbar.
drawingManager = new atlas.drawing.DrawingManager(map, {
toolbar: new atlas.control.DrawingToolbar({
buttons: ['draw-line'],
position: 'top-left',
style: 'light'
})
});
///Clear the map and drawing canvas when the user enters into a drawing mode.
map.events.add('drawingmodechanged', drawingManager, drawingModeChanged);
//Monitor for when a line drawing has been completed.
map.events.add('drawingcomplete', drawingManager, snapLineToRoute);
});
}
function drawingModeChanged(mode) {
//Clear the drawing canvas and data source.
if (mode.startsWith('draw')) {
drawingManager.getSource().clear();
datasource.clear();
}
}
function snapLineToRoute(line) {
//TODO: Work around: unwrap timeout once race condition bug is fixed.
setTimeout(function () {
//Exit drawing mode.
drawingManager.setOptions({ mode: 'idle' });
}, 1);
//Get the coordinates from the drawn line.
var lineCoords = line.getCoordinates();
//Create an array of GeoJSON supporting points from the line coordinates.
var supportingPoints = lineCoords.map((val) => {
return new atlas.data.Point(val)
});
//When reconstructing a route, the start and end coordinates must be specified in the query.
var coordinates = [lineCoords[0], lineCoords[lineCoords.length - 1]];
//Pass all points to reconstruct the route and create a logical path as supporting points in the body of the request.
var options = {
postBody: {
"supportingPoints": {
"type": "GeometryCollection",
"geometries": supportingPoints
}
}
};
//Calculate a route.
routeURL.calculateRouteDirections(atlas.service.Aborter.timeout(10000), coordinates, options).then((directions) => {
//Get the logical route path as GeoJSON and add it to the data source.
var data = directions.geojson.getFeatures();
datasource.add(data);
//Update the map view to center over the route.
map.setCamera({
bounds: data.bbox,
padding: 30 //Add a padding to account for the pixel size of symbols.
});
});
}
</script>
</head>
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend><h1 style="font-size:16px">Snap drawn line to roads</h1></legend>
This sample shows how to snap a line drawn using the draiwng tools to the road network.
To run this sample, press the line drawing button in the top corner of the map and draw a line near roads.
When you complete the drawing (double click, press ESC, or press the line drawing button again),
the Azure Maps route direction service will be used to calculate logical route path on the road network following the drawn line.
The black line is the original drawn line and the blue line is the snapped line on the road network.
<br/><br/>
<b>Modules used:</b> Drawing, Services
</fieldset>
</body>
</html>
You can’t perform that action at this time.