/
Select Pushpins Along a Route.html
133 lines (107 loc) · 5.86 KB
/
Select Pushpins Along a Route.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
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE html>
<html lang="en">
<head>
<title>Select Pushpins Along a Route - 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 use the Spatial Math and Directions module to calculate which pushpins are within 1 mile of a route." />
<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, pinLayer, directionsManager;
function GetMap() {
map = new Microsoft.Maps.Map('#myMap', {
center: new Microsoft.Maps.Location(47.634003, -122.229652),
zoom: 12
});
//Add some random pushpins to a layer on the map.
pinLayer = new Microsoft.Maps.Layer();
map.layers.insert(pinLayer);
var pushpins = Microsoft.Maps.TestDataGenerator.getPushpins(100, map.getBounds(), { color: 'purple' });
pinLayer.add(pushpins);
//Load the directions and Spatial Math modules.
Microsoft.Maps.loadModule(['Microsoft.Maps.Directions', 'Microsoft.Maps.SpatialMath'], function () {
//Create an instance of the directions manager.
directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
directionsManager.setRequestOptions({
maxRoutes: 1
});
//Create waypoints to route between.
directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'Seattle, WA' }));
directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'Redmond, WA' }));
//Add an event for when the directions are updated to calculate the distance.
Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', directionsUpdated);
//Calculate directions.
directionsManager.calculateDirections();
});
}
function directionsUpdated() {
if (directionsManager) {
var route = directionsManager.getCurrentRoute();
if (route) {
var routePath = new Microsoft.Maps.Polyline(route.routePath);
var searchArea = Microsoft.Maps.SpatialMath.Geometry.buffer(routePath, 1, Microsoft.Maps.Directions.DistanceUnit.miles);
findIntersectingData(searchArea);
}
}
}
//Find all pushpins on the map that intersect with the search area.
function findIntersectingData(searchArea) {
if (searchArea) {
//Get all the pushpins from the pinLayer.
var pins = pinLayer.getPrimitives();
//Clear any previously selected data.
for (var i = 0; i < pins.length; i++) {
pins[i].setOptions({ color: 'purple' });
}
//Using spatial math find all pushpins that intersect with the drawn search area.
//The returned data is a copy of the intersecting data and not a reference to the original shapes,
//so making edits to them will not cause any updates on the map.
var intersectingPins = Microsoft.Maps.SpatialMath.Geometry.intersection(pins, searchArea);
//The data returned by the intersection function can be null, a single shape, or an array of shapes.
if (intersectingPins) {
//For ease of usem wrap individudal shapes in an array.
if (intersectingPins && !(intersectingPins instanceof Array)) {
intersectingPins = [intersectingPins];
}
var selectedPins = [];
//Loop through and map the intersecting pushpins back to their original pushpins by comparing their coordinates.
for (var j = 0; j < intersectingPins.length; j++) {
for (var i = 0; i < pins.length; i++) {
if (Microsoft.Maps.Location.areEqual(pins[i].getLocation(), intersectingPins[j].getLocation())) {
//Set the selected pin color to red.
pins[i].setOptions({ color: 'red' });
selectedPins.push(pins[i]);
break;
}
}
}
//Return the pushpins that were selected.
return selectedPins;
}
}
return null;
}
</script>
</head>
<body>
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;background-color:gray"></div><br />
<div id="outputPanel"></div>
<fieldset style="width:800px;margin-top:10px;">
<legend>Select Pushpins Along a Route Sample</legend>
This sample shows how to use the Spatial Math and Directions module to calculate which pushpins are within 1 mile of a route.
</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>