-
Notifications
You must be signed in to change notification settings - Fork 449
/
Create a layer control.html
309 lines (250 loc) · 13.1 KB
/
Create a layer control.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!DOCTYPE html>
<html lang="en">
<head>
<title>Create a layer control - 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 loads Mapbox styles of a GeoJSON polygon into Azure Maps." />
<meta name="keywords" content="Microsoft maps, maps, map, API, SDK, GIS, layer 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>
<script>
var map, legend, layerControl;
function getMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
view: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
//Use Microsoft Entra ID authentication.
authType: 'anonymous',
clientId: 'e6b6ab59-eb5d-4d25-aa57-581135b927f0', //Your Azure Maps client id for accessing your Azure Maps account.
getToken: function (resolve, reject, map) {
//URL to your authentication service that retrieves an Microsoft Entra ID Token.
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsToken';
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: 'Legend'
});
//Add the legend control to the map.
map.controls.add(legend, {
position: 'bottom-left'
});
//Create the layer control.
layerControl = new atlas.control.LayerControl({
title: 'Layer options',
//Attach the legend control.
legendControl: legend,
dynamicLayerGroup: {
groupTitle: 'Layers',
layout: 'checkbox'
},
//Specify the layer groups for the control.
layerGroups: [
//A dropdown layer group option.
{
layout: 'dropdown',
//A title for this layer group.
groupTitle: 'Symbol label',
//The layers affected by this group.
layers: ['earthquake-labels'],
items: [{
label: 'mag',
//Optionally specify if the state is enabled or not initially.
enabled: true,
//The style to apply to the layers when this state is enabled.
enabledStyle: {
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']
}
}
}, {
label: 'place',
//The style to apply to the layers when this state is enabled.
enabledStyle: {
textOptions: {
textField: ['get', 'place']
}
}
}]
},
//A group of check box options.
{
layout: 'checkbox',
//A title for this layer group.
groupTitle: 'Magnitude filter',
//The layers affected by this group.
layers: ['earthquake-circles', 'earthquake-labels'],
items: [
{
layout: 'checkbox',
label: 'mag > 6',
//Optionally specify if the state is enabled or not initially.
enabled: false,
//Style to apply when a checkbox is checked.
enabledStyle: {
filter: ['>', ['get', 'mag'], 6]
},
//Style to apply when a checkbox is unchecked.
disabledStyle: {
filter: null
}
}
]
},
//A range sliders layer group option.
{
layout: 'range',
//A title for this layer group, in this case using a value that has a match in the localization resources (resx).
groupTitle: 'rangeSlider',
//The layers affected by this group.
layers: ['earthquake-circles'],
//Settings for one or more range sliders.
items: [{
//Optionally format the label.
label: '{rangeValue}px',
//Set the initial value.
value: 0.5,
//Specify the style applied to the layers as the range sliders value changes.
style: {
opacity: '{rangeValue}',
//Use a style expression to apply custom logic, in this case, applying the opposite value to the stroke.
strokeOpacity: ['-', 1, '{rangeValue}']
},
//Specify is the layer styles should update as the slider is moving, rather than waiting until it has finished moving.
updateOnInput: true
}
]
},
//A radio button layer group options.
{
layout: 'radio',
//A title for this layer group.
groupTitle: 'Stroke Width',
//The layers affected by this group.
layers: ['earthquake-circles'],
items: [{
label: '5px',
//Style to apply when a radio item is selected.
enabledStyle: {
strokeWidth: 5
}
},
{
label: '10px',
//Style to apply when a radio item is selected.
enabledStyle: {
strokeWidth: 10
},
//Optionally limit the zoom level that certain options appear.
//minZoom: 3,
//maxZoom: 7
},
{
label: '15px',
//Style to apply when a radio item is selected.
enabledStyle: {
strokeWidth: 15
}
}
]
}],
resx: {
'rangeSlider': 'Opacity slider'
}
});
//Add the layer control to the map.
map.controls.add(layerControl, {
position: 'top-right'
});
//Optionally add events to the layer control to know when any states change.
map.events.add('statechanged', layerControl, function (e) {
console.log('statechanged');
});
map.events.add('toggled', legend, function (e) {
console.log('Minified ' + e.minimized);
});
//Load some mock data for the example.
loadMockData();
});
}
function loadMockData(){
//Create a data source and add it to the map.
var source = new atlas.source.DataSource();
map.sources.add(source);
//Load the earthquake data.
source.importDataFromUrl('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.geojson');
map.layers.add([
//Create a layer that defines how to render the shapes in the data source and add it to the map.
new atlas.layer.BubbleLayer(source, '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
]
}),
//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(source, '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
}
})
]);
}
</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">Create a layer control</h1>
</legend>
This sample shows how to create a layer control and add it to the map.
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>