This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Draw Search Area.html
122 lines (103 loc) · 5.24 KB
/
Draw Search Area.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="en">
<head>
<title>Draw Search Area - Bing Maps Samples</title>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/favicon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample shows how to combine the drawing tools with the spatial math module together filter pushpins that intersect with a drawn area." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, Bing, Bing Maps" />
<meta name="author" content="Microsoft Bing Maps" />
<meta name="screenshot" content="screenshot.jpg" />
<script>
var map, drawingManager;
//Query URL to the Fourth Coffe Shop data source
var sdsDataSourceUrl = 'http://spatial.virtualearth.net/REST/v1/data/20181f26d9e94c81acdf9496133d4f23/FourthCoffeeSample/FourthCoffeeShops';
function GetMap() {
map = new Microsoft.Maps.Map('#myMap', {
center: new Microsoft.Maps.Location(47.6088, -122.3381),
zoom: 13
});
//Load the DrawingTools, SpatialDataServices modules.
Microsoft.Maps.loadModule(['Microsoft.Maps.DrawingTools', 'Microsoft.Maps.SpatialDataService'], function () {
//Create an instance of the DrawingTools class and bind it to the map.
var tools = new Microsoft.Maps.DrawingTools(map);
//Show the drawing toolbar and enable editting on the map.
tools.showDrawingManager(function (manager) {
//Store a reference to the drawing manager as it will be useful later.
drawingManager = manager;
//Limit which tools appear in the drawing toolbar.
manager.setOptions({
drawingBarActions: Microsoft.Maps.DrawingTools.DrawingBarAction.polygon
});
//Add an event for when a drawing has ended.
Microsoft.Maps.Events.addHandler(manager, 'drawingEnded', drawingChangedHandler);
//Add an event for when a drawing has ended.
Microsoft.Maps.Events.addHandler(manager, 'drawingModeChanged', drawingModeChangedHandler);
})
});
}
function drawingModeChangedHandler(mode) {
//If the user decides to draw a new shape, clear the drawing layer.
if (mode != Microsoft.Maps.DrawingTools.DrawingMode.none &&
mode != Microsoft.Maps.DrawingTools.DrawingMode.erase &&
mode != Microsoft.Maps.DrawingTools.DrawingMode.edit) {
//User must have set it to draw a new shape.
//Delete any current shapes in the drawing layer.
drawingManager.clear();
}
}
function drawingChangedHandler(shape) {
//Remove any existing data from the map.
map.entities.clear();
//If querying a spatial database like SQL, use the SpatialMath module and ensure the shape is valid.
//This step isn't needed when using the Spatial Data Services.
//shape = Microsoft.Maps.SpatialMath.Geometry.makeValid(shape);
if (shape instanceof Microsoft.Maps.Polygon) {
//Ensure polygons are closed.
var locs = shape.getLocations();
if (!Microsoft.Maps.Location.areEqual(locs[0], locs[locs.length - 1])) {
locs.push(locs[0]);
}
shape.setLocations(locs);
}
var shapeWkt = Microsoft.Maps.WellKnownText.write(shape);
if (shapeWkt) {
//Create a query to get data that intrsects the bounds of the map.
var queryOptions = {
queryUrl: sdsDataSourceUrl,
top: 250,
spatialFilter: {
spatialFilterType: 'intersects',
intersects: shapeWkt
}
};
//Process the query.
Microsoft.Maps.SpatialDataService.QueryAPIManager.search(queryOptions, map, function (data) {
//Add results to the map.
map.entities.push(data);
});
}
}
</script>
</head>
<body>
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;background-color:gray"></div>
<fieldset style="width:800px;margin-top:10px;">
<legend>Draw Search Area Sample</legend>
This sample shows how to combine the drawing tools with the spatial math module together filter pushpins that intersect with a drawn area.
The pushpins that are filtered will change color to demonstrate that they were filtered. <br /><br />
To try this demo, press the polygon button, draw a polygon, when done, press ESC or click the polygon button again.
</fieldset>
<script>
// Dynamic load the Bing Maps Key and Script
// Get your own Bing Maps key at https://www.microsoft.com/maps
(async () => {
let script = document.createElement("script");
let bingKey = await fetch("https://samples.azuremaps.com/api/GetBingMapsKey").then(r => r.text()).then(key => { return key });
script.setAttribute("src", `https://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=${bingKey}`);
document.body.appendChild(script);
})();
</script>
</body>
</html>