Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
<!DOCTYPE html>
<html lang="en">
<head>
<title>Select data in drawn polygon area - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/favicon.ico"/>
<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 use the drawing tools to draw polygon areas on the map and select points that are within them. " />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, drawing tools, circle, rectangle, polygon, paint, events, within, intersects, intersection" />
<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/1/atlas-drawing.min.css" type="text/css" />
<script src="https://atlas.microsoft.com/sdk/javascript/drawing/1/atlas-drawing.min.js"></script>
<!-- Load turf.js a spatial math library. https://turfjs.org/ -->
<script src='/Common/scripts/turf.min.js'></script>
<script type='text/javascript'>
var map, datasource, drawingManager, points, layer;
//GeoJSON feed that contains the data we want to map.
var geojsonFeed = '../Common/data/geojson/SamplePoiDataSet.json';
function GetMap() {
//Point the Azure Maps domain to the US Azure Gov Cloud domain.
atlas.setDomain('atlas.azure.us');
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-90, 35],
zoom: 3,
view: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
//Use Azure Active Directory authentication.
authType: 'anonymous',
clientId: 'c9f2f391-13f1-407b-a4a5-f0a241bacfbf', //Your Azure Active Directory client id for accessing your Azure Maps account.
getToken: function (resolve, reject, map) {
//URL to your authentication service that retrieves an Azure Active Directory Token.
var tokenServiceUrl = 'https://azuremapscodesamples.azurewebsites.us/Common/TokenService.ashx';
fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
}
//Alternatively, use an Azure Maps key. Get an Azure Maps key at https://azure.com/maps. NOTE: The primary key should be used as the key.
//authType: 'subscriptionKey',
//subscriptionKey: '<Your Azure Maps Key>'
}
});
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
map.sources.add(datasource);
//Import the GeoJSON data into the data source. Capture the point data after it has loaded for quick lookups later.
datasource.importDataFromUrl(geojsonFeed).then(() => {
points = datasource.toJson();
});
//Create a layer to render the points.
layer = new atlas.layer.BubbleLayer(datasource, null, {
color: '#3399ff'
});
map.layers.add(layer);
//Create an instance of the drawing manager and display the drawing toolbar.
drawingManager = new atlas.drawing.DrawingManager(map, {
toolbar: new atlas.control.DrawingToolbar({
buttons: ['draw-polygon', 'draw-rectangle', 'draw-circle', 'edit-geometry'],
position: 'top-right',
style: 'light'
})
});
//Hide the polygon fill area as only want to show outline of search area.
drawingManager.getLayers().polygonLayer.setOptions({ visible: false });
//Clear the map and drawing canvas when the user enters into a drawing mode.
map.events.add('drawingmodechanged', drawingManager, drawingModeChanged);
//Monitor for when a polygon drawing has been completed.
map.events.add('drawingcomplete', drawingManager, searchPolygon);
});
}
function drawingModeChanged(mode) {
//Clear the drawing canvas when the user enters into a drawing mode and reset the style of all pins.
if (mode.startsWith('draw')) {
drawingManager.getSource().clear();
//Update the color option of the layer such that all points have the same color.
layer.setOptions({ color: '#3399ff' });
}
}
function searchPolygon(searchArea) {
//Exit drawing mode.
drawingManager.setOptions({ mode: 'idle' });
//If the search area is a circle, create a polygon from its circle coordinates.
if (searchArea.isCircle()) {
searchArea = new atlas.Shape(new atlas.data.Polygon([searchArea.getCircleCoordinates()]));
}
var poly = searchArea.toJson();
//Update the color option of the layer and use the 'within' expression to change the color of all points that are within the search area.
layer.setOptions({
color: [
'case',
['within', poly],
'red',
'#3399ff'
]
});
//Optionally, retrieve the individual points that are in the search area.
//Calculate all points that are within the polygon area.
var ptsWithin = turf.pointsWithinPolygon(points, poly);
//Do something with the selected points.
//For demo purposes, we will simply output the name of each pin.
var html = [ptsWithin.length, ' Pins Selected:<br/><br/>'];
for (var i = 0; i < ptsWithin.length; i++) {
html.push(ptsWithin[i].properties.name, '<br/>');
}
document.getElementById('output').innerHTML = html.join('');
}
</script>
<style>
#myMap {
position: relative;
width: calc(100% - 210px);
min-width: 290px;
height: 600px;
float: left;
}
#output {
float: left;
width: 200px;
height: 600px;
margin-left: 10px;
overflow-y: auto;
}
</style>
</head>
<body onload="GetMap()">
<div id="myMap"></div>
<div id="output"></div>
<div style="clear:both"></div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend><h1 style="font-size:16px">Select data in drawn polygon area</h1></legend>
This sample shows how to use the drawing tools to draw polygon areas on the map and select points that are within them.
This sample uses the open source <a href="https://turfjs.org/" target="_blank">Turf.js</a> library to for some of the spatial calculation.
</fieldset>
</body>
</html>