-
Notifications
You must be signed in to change notification settings - Fork 471
Expand file tree
/
Copy pathDynamic legend and layer controls.html
More file actions
245 lines (205 loc) · 10.5 KB
/
Dynamic legend and layer controls.html
File metadata and controls
245 lines (205 loc) · 10.5 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dynamic legend and layer controls - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample shows how to create use the dynamic legend and layer controls." />
<meta name="keywords" content="Microsoft maps, maps, map, API, SDK, GIS, legend, legend control" />
<meta name="author" content="Microsoft Azure Maps" /><meta name="version" content="1.0" />
<meta name="screenshot" content="screenshot.jpg" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.css" rel="stylesheet" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.js"></script>
<!-- Load in the JavaScript and CSS files that has our custom control. -->
<link rel="stylesheet" href="/lib/azure-maps/azure-maps-layer-legend.min.css" type="text/css" />
<script src="/lib/azure-maps/azure-maps-layer-legend.min.js"></script>
<!-- Add reference to the Azure Maps Spatial IO module. -->
<script src="https://atlas.microsoft.com/sdk/javascript/spatial/0/atlas-spatial.min.js"></script>
<script>
var map, legend;
function getMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
view: 'Auto',
language: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
// Use SAS token for authentication
authType: 'sas',
getToken: function (resolve, reject, map) {
// URL to your authentication service that retrieves a SAS Token
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsSasToken';
fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
}
//Alternatively, use an Azure Maps key. Get an Azure Maps key at https://azure.com/maps. NOTE: The primary key should be used as the key.
//authType: 'subscriptionKey',
//subscriptionKey: '[YOUR_AZURE_MAPS_KEY]'
}
});
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a legend control that the layer control will leverage.
legend = new atlas.control.LegendControl({
title: 'My Legend',
//Optional resource name mappings for named values that appear in some dynamically created legends.
resx: {
low: 'Low',
high: 'High',
'no-data': 'No data'
}
});
//Add the legend control to the map.
map.controls.add(legend, {
position: 'bottom-left'
});
//Create the layer control.
layerControl = new atlas.control.LayerControl({
//Attach the legend control.
legendControl: legend,
//Set dynamic layer group settings.
dynamicLayerGroup: {
groupTitle: 'Layers',
layout: 'checkbox'
}
});
//Add the layer control to the map.
map.controls.add(layerControl, {
position: 'top-right'
});
//Load some mock data for the example.
loadMockData();
});
}
function loadMockData(){
//Load a couple images for testing.
var images = [
map.imageSprite.createFromTemplate('marker-arrow', 'marker-arrow', 'purple'),
map.imageSprite.createFromTemplate('marker-square', 'marker-square', 'red'),
map.imageSprite.createFromTemplate('marker-ball-pin', 'marker-ball-pin', 'orange')
];
Promise.all(images).then(() => {
//Load a data set of points, in this case earthquake data from the USGS.
var earthquakeSource = new atlas.source.DataSource();
map.sources.add(earthquakeSource);
earthquakeSource.importDataFromUrl('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson');
//Load US state boundaries.
var stateSource = new atlas.source.DataSource();
map.sources.add(stateSource);
stateSource.importDataFromUrl('/data/geojson/US_States_Population_Density.json');
//Load POI points.
var poiSource = new atlas.source.DataSource();
map.sources.add(poiSource);
poiSource.importDataFromUrl('/data/geojson/SamplePoiDataSet.json');
//Load a KML source.
var kmlSource = new atlas.source.DataSource();
map.sources.add(kmlSource);
map.layers.add([
//Create a heatmap and add it to the map.
new atlas.layer.HeatMapLayer(earthquakeSource, 'Earthquake Heatmap', {
radius: 10,
opacity: 0.8,
maxZoom: 5
}),
//Add a choropleth polygon layer
new atlas.layer.PolygonLayer(stateSource, 'State population density', {
fillColor: [
'step',
['get', 'density'],
'#FFEDA0',
10, '#FED976',
20, '#FEB24C',
50, '#FD8D3C',
100, '#FC4E2A',
200, '#E31A1C',
500, '#BD0026',
1000, '#800026'
],
maxZoom: 5
}),
//Add an OGC layer
new atlas.layer.OgcMapLayer({
url: 'https://mrdata.usgs.gov/services/gscworld',
activeLayers: ['geology'],
minZoom: 5
})
], 'labels');
map.layers.add([
new atlas.layer.BubbleLayer(earthquakeSource, 'Earthquake Circles', {
//Bubbles are made semi-transparent.
opacity: 0.75,
createIndicators: true, // to enable bubble layer a11y feature
//Color of each bubble based on the value of "mag" property using a color gradient of green, yellow, orange, and red.
color: [
'interpolate',
['linear'],
['get', 'mag'],
0, 'green',
5, 'yellow',
6, 'orange',
7, 'red'
],
/*
* Radius for each data point scaled based on the value of "mag" property.
* When "mag" = 0, radius will be 2 pixels.
* When "mag" = 8, radius will be 40 pixels.
* All other "mag" values will be a linear interpolation between these values.
*/
radius: [
'interpolate',
['linear'],
['get', 'mag'],
0, 2,
8, 40
],
minZoom: 5
}),
//Create a symbol layer using the same data source to render the magnitude as text above each bubble and add it to the map.
new atlas.layer.SymbolLayer(earthquakeSource, 'Earthquake Labels', {
iconOptions: {
//Hide the icon image.
image: 'none'
},
textOptions: {
//An expression is used to concerte the "mag" property value into a string and appends the letter "m" to the end of it.
textField: ['concat', ['to-string', ['get', 'mag']], 'm'],
textSize: 12
},
minZoom: 5
}),
//Load a layer that has several different icons
new atlas.layer.SymbolLayer(poiSource, 'POIs', {
iconOptions: {
//Use a match expression to select the image icon based on the EntityType property of the data point.
image: [
'match',
['get', 'EntityType'],
//For each entity type, specify the icon name to use.
'Gas Station', 'marker-ball-pin',
'Grocery Store', 'marker-arrow',
'Restaurant', 'marker-square',
'School', 'pin-round-blue',
//Default fallback icon.
'marker-black'
]
},
minZoom: 5
})
]);
});
}
</script>
</head>
<body onload="getMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>
<h1 style="font-size:16px">Dynamic legend and layer controls</h1>
</legend>
This sample shows how to create use the dynamic legend and layer controls.
This will automatically detect user added layers in the map and generate a layer selection control and legend.
This sample uses the open source <a href="https://github.com/Azure-Samples/azure-maps-layer-legend" target="_blank">Azure Maps Layer & Legend Control module</a>
</fieldset>
<div id="outsidePanel"></div>
</body>
</html>