-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleLayer.html
166 lines (149 loc) · 8.45 KB
/
BubbleLayer.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html>
<head>
<title>Point Feature Clustering - Azure Maps Web Control 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 enable point based clustering on a data source and render them differently from symbols." />
<meta name="keywords" content="map, gis, API, SDK, markers, pins, pushpins, symbols, layer, bubbles, clustering, superclusterer" /> -->
<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/css/atlas.min.css?api-version=1" type="text/css" />
<script src="https://atlas.microsoft.com/sdk/js/atlas.min.js?api-version=1"></script>
<script type='text/javascript'>
var map, datasource, popup;
//GeoJSON feed of all earthquakes from the past 30 days. Sourced from the USGS.
var earthquakeFeed = "https://us-central1-cruzhacks.cloudfunctions.net/geojson";
function pickSix(inputString)
{
var splitted = inputString.split(",");
if(splitted.length < 6)
return inputString;
return `${splitted[0]},${splitted[1]},${splitted[2]},${splitted[3]},${splitted[4]},${splitted[5]}`;
}
//Define an HTML template for a custom popup content laypout.
var popupTemplate = '<div class="customInfobox"><div class="name">{name}</div>{description}<div>{label}</div></div>';
function GetMap() {
//Add your Azure Maps subscription key to the map SDK. Get an Azure Maps key at https://azure.com/maps
atlas.setSubscriptionKey('fQy_PGJRvRsW9e9sgPuXaa4TlYisE7wDXrRKTHvaOBA');
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-97, 39],
zoom: 3,
style: 'day'
});
//Wait until the map resources have fully loaded.
map.events.add('load', function (e) {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource(null, {
//Tell the data source to cluster point data.
cluster: true,
//The radius in pixels to cluster points together.
clusterRadius: 45,
//The maximium zoom level in which clustering occurs.
//If you zoom in more than this, all points are rendered as symbols.
clusterMaxZoom: 15
});
map.sources.add(datasource);
//Create a bubble layer for rendering clustered data points.
var clusterBubbleLayer = new atlas.layer.BubbleLayer(datasource, null, {
//Scale the size of the clustered bubble based on the number of points inthe cluster.
radius: [
'step',
['get', 'point_count'],
20, //Default of 20 pixel radius.
5, 50, //If point_count >= 100, radius is 30 pixels.
10, 70 //If point_count >= 750, radius is 40 pixels.
],
//Change the color of the cluster based on the value on the point_cluster property of the cluster.
color: [
'step',
['get', 'point_count'],
'rgba(0,255,0,0.8)', //Default to green.
5, 'rgba(255,255,0,0.8)', //If the point_count >= 100, color is yellow.
10, 'rgba(255,0,0,0.8)' //If the point_count >= 100, color is red.
],
strokeWidth: 0,
filter: ['has', 'point_count'] //Only rendered data points which have a point_count property, which clusters do.
});
//Add a click event to the layer so we can zoom in when a user clicks a cluster.
map.events.add('click', clusterBubbleLayer, symbolClicked);
//Add mouse events to change the mouse cursor when hovering over a cluster.
map.events.add('mouseenter', clusterBubbleLayer, function () {
map.getCanvas().style.cursor = 'pointer';
});
map.events.add('mouseleave', clusterBubbleLayer, function () {
map.getCanvas().style.cursor = '';
});
var symbolLayer = new atlas.layer.SymbolLayer(datasource);
//Add the clusterBubbleLayer and two additional layers to the map.
map.layers.add([
clusterBubbleLayer,
//Create a symbol layer to render the count of locations in a cluster.
new atlas.layer.SymbolLayer(datasource, null, {
iconOptions: {
image: 'none' //Hide the icon image.
},
textOptions: {
textField: '{point_count_abbreviated}',
offset: [0, 0.4]
}
}),
//Create a layer to render the individual locations.
new atlas.layer.SymbolLayer(datasource, null, {
filter: ['!', ['has', 'point_count']] //Filter out clustered points from this layer.
}),
symbolLayer
]);
//Retrieve a GeoJSON data set and add it to the data source.
datasource.importDataFromUrl(earthquakeFeed);
// var symbolLayer = new atlas.layer.SymbolLayer(datasource);
// map.layers.add(symbolLayer);
popup = new atlas.Popup({
position: [0, 0],
pixelOffset: [0, -18]
});
//Add a click event to the symbol layer.
map.events.add('click', symbolLayer, symbolClicked);
});
}
function symbolClicked(e) {
//Make sure the event occured on a point feature.
if (e.shapes && e.shapes.length > 0) {
var content, coordinate;
//Check to see if the first value in the shapes array is a Point Shape.
if (e.shapes[0] instanceof atlas.Shape && e.shapes[0].getType() === 'Point') {
var properties = e.shapes[0].getProperties();
content = popupTemplate.replace(/{name}/g, properties.POI).replace(/{description}/g, "Reports: "+properties.INSTANCES).replace(/{label}/g, pickSix(properties.LABEL));
coordinate = e.shapes[0].getCoordinates();
} else if (e.shapes[0].type === 'Feature' && e.shapes[0].geometry.type === 'Point') {
//Check to see if the feature is a cluster.
if (e.shapes[0].properties.cluster) {
content = '<div style="padding:10px;">Cluster of ' + e.shapes[0].properties.point_count + ' breach points</div>';
} else {
//Feature is likely from a VectorTileSource.
content = popupTemplate.replace(/{name}/g, properties.name).replace(/{description}/g, properties.description);
}
coordinate = e.shapes[0].geometry.coordinates;
}
if (content && coordinate) {
//Populate the popupTemplate with data from the clicked point feature.
popup.setOptions({
//Update the content of the popup.
content: content,
//Update the position of the popup with the symbols coordinate.
position: coordinate
});
//Open the popup.
popup.open(map);
}
}
}
</script>
</head>
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;min-width:360px;height:810px;"></div>
<!-- <fieldset style="width:calc(100% - 30px);min-width:350px;margin-top:10px;">
</fieldset> -->
</body>