Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
170 lines (137 sloc) 6.99 KB
<!DOCTYPE html>
<html lang="en">
<head>
<title>Select data in drawn polygon area - 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 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/0/atlas-drawing.min.css" type="text/css" />
<script src="https://atlas.microsoft.com/sdk/javascript/drawing/0/atlas-drawing.min.js"></script>
<!-- Load turf.js a spatial math library. https://turfjs.org/ -->
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
<script type='text/javascript'>
var map, datasource, drawingManager, points;
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 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>'
}
});
//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);
//Generate 1000 random points within the bounds of the map view using the Turf.js library
points = turf.randomPoint(1000, {
bbox: map.getCamera().bounds
});
//Assign a color and name property to each point.
for (var i = 0; i < points.features.length; i++) {
points.features[i].properties = {
name: 'Pin_' + i,
color: '#3399ff'
};
}
//Add the points to the map.
datasource.add(points);
//Create a layer to render the points.
map.layers.add(new atlas.layer.BubbleLayer(datasource, null, {
color: ['get', 'color']
}));
//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'],
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();
//Reset the color property of each point.
for (var i = 0; i < points.features.length; i++) {
points.features[i].properties.color = '#3399ff';
}
//Update the data on the map.
datasource.setShapes(points);
}
}
function searchPolygon(searchArea) {
//Exit drawing mode.
drawingManager.setOptions({ mode: 'idle' });
var poly = searchArea.toJson();
//If the search area is a circle, create a polygon from its circle coordinates.
if (searchArea.isCircle()) {
poly = new atlas.data.Polygon([searchArea.getCircleCoordinates()]);
}
//Calculate all points that are within the polygon area.
var ptsWithin = turf.pointsWithinPolygon(points, poly);
//Change to color of all points that are in the polygon area to red.
for (var i = 0; i < ptsWithin.features.length; i++) {
ptsWithin.features[i].properties.color = 'red';
}
//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('');
//Update the points on the map since the color property has changed.
datasource.setShapes(points);
}
</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>
You can’t perform that action at this time.