/
Geometry Binary Operations.html
97 lines (87 loc) · 4.08 KB
/
Geometry Binary Operations.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Geometry Binary Operations - 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 example loads a map with two random polygons where you can try different binary operations to be performed against the polygons." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, Bing, Bing Maps" />
<meta name="author" content="Microsoft Bing Maps" />
<meta name="screenshot" content="bmv8-spatialmathintersectionexample.jpg" />
<script>
var map, polygons, result;
function GetMap() {
map = new Microsoft.Maps.Map('#myMap', {
center: new Microsoft.Maps.Location(43, -107.55),
zoom: 15
});
//Load the Spatial Math module.
Microsoft.Maps.loadModule('Microsoft.Maps.SpatialMath', function () {
//Generate two random polygons to test the geometry calcuations with.
polygons = Microsoft.Maps.TestDataGenerator.getPolygons(2, map.getBounds(), null, 0.6, null, true);
map.entities.push(polygons);
});
}
function processOperation(operation) {
//If a result is already on the map. Clear the map and re-add the test polygons.
if (result) {
map.entities.clear();
map.entities.push(polygons);
}
//Apply the requested geometry calculation.
switch (operation) {
case 'difference':
result = Microsoft.Maps.SpatialMath.Geometry.difference(polygons[0], polygons[1]);
break;
case 'intersection':
result = Microsoft.Maps.SpatialMath.Geometry.intersection(polygons[0], polygons[1]);
break;
case 'union':
result = Microsoft.Maps.SpatialMath.Geometry.union(polygons[0], polygons[1]);
break;
case 'symDifference':
result = Microsoft.Maps.SpatialMath.Geometry.symDifference(polygons[0], polygons[1]);
break;
case 'unionAggregate':
result = Microsoft.Maps.SpatialMath.Geometry.unionAggregate(polygons);
break;
}
if (result) {
//Outline the resulting shape and make the fill area transparent so that the original shapes can still be seen.
if (result.length !== undefined) {
for (var i = 0, len = result.length; i < len; i++) {
result[i].setOptions({ strokeColor: 'red', fillColor: 'transparent', strokeThickness: 5 });
}
}
else {
result.setOptions({ strokeColor: 'red', fillColor: 'transparent', strokeThickness: 5 });
}
map.entities.push(result);
}
else {
alert('Unable to find a result.');
}
}
</script>
</head>
<body>
<div id='myMap' style='position:relative;width:600px;height:400px;'></div>
<br/>
<input type="button" value="Difference" />
<input type="button" value="Intersection" />
<input type="button" value="Union" />
<input type="button" value="Union Aggregate" />
<input type="button" value="SymDifference" />
<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>