-
Notifications
You must be signed in to change notification settings - Fork 449
/
Create a legend control.html
407 lines (363 loc) · 18.3 KB
/
Create a legend 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<!DOCTYPE html>
<html lang="en">
<head>
<title>Create a legend 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="TThis sample shows how to create a legend control and add it to the map." />
<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>
<script>
var map, legend;
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 () {
//Add the custom control to the map.
legend = new atlas.control.LegendControl({
//Global title to display for the legend.
title: 'My Legend',
//How the legend control should layout multiple legend cards. Options: 'list' | 'carousel' | 'accordion'
// layout: 'accordion',
//container: 'outsidePanel',
//All legend cards to display within the legend control.
legends: [
//A category legend that uses a combination of shapes and icons.
{
type: 'category',
subtitle: 'Category',
layout: 'column',
itemLayout: 'row',
footer: 'A category legend that uses a combination of shapes and icons.',
strokeWidth: 2,
items: [
{
color: 'DodgerBlue',
label: 'label1',
//Url to an image.
shape: '/images/icons/campfire.png',
alt: 'campfire'
}, {
color: 'Yellow',
label: 'label2',
shape: 'square'
}, {
color: 'Orange',
label: 'Ricky',
shape: 'line'
}, {
color: 'Red',
label: 'is',
shape: 'circle'
}, {
color: 'purple',
label: 'awesome!',
shape: 'triangle'
}
]
},
//A category legend that scales the shapes/icons.
{
type: 'category',
subtitle: 'Category - Scaled shapes',
layout: 'column-reverse',
itemLayout: 'row',
shape: 'circle',
color: 'transparent',
footer: 'A category legend that scales the shapes/icons.',
//Setting fitItems to true will allow all shapes to be equally spaced out and centered.
fitItems: true,
//Set the shape size for each item.
items: [
{
label: '10',
shapeSize: 10
}, {
label: '20',
shapeSize: 20
}, {
label: '30',
shapeSize: 30
}, {
label: '40',
shapeSize: 40
}
]
},
//A category legend with different size lines.
{
type: 'category',
subtitle: 'Category - Scaled lines',
layout: 'column-reverse',
itemLayout: 'row',
shape: 'line',
footer: 'A category legend that scales lines. Scale line thickness by using stroke width. Use shape size to specify length.',
//Setting fitItems to true will allow all shapes to be equally spaced out and centered.
fitItems: true,
//Use shape size to set line length.
shapeSize: 20,
//Scale lines by using stroke width.
items: [
{
label: '1',
strokeWidth: 1,
color: 'red'
}, {
label: '2',
strokeWidth: 2,
color: 'orange'
}, {
label: '4',
strokeWidth: 4,
color: 'purple'
}, {
label: '6',
strokeWidth: 6,
color: 'blue'
}
]
},
//A category legend that collapses the space between items, overlaps the labels with the shapes, and uses custom CSS to collapse the space between the category items.
{
type: 'category',
subtitle: 'Category - Custom CSS',
layout: 'column',
itemLayout: 'column',
shape: 'square',
//Have the labels overlap the shape. Otherwise the text span size may push the shape away from the other shapes.
labelsOverlapShapes: true,
//Collapse the space between the items.
collapse: true,
//Optionally remove the stroke around the shapes.
strokeWidth: 0,
footer: 'A category legend that collapses the space between items, overlaps the labels with the shapes, and uses custom CSS to collapse the space between the category items.',
items: [
{
label: 'low',
color: 'white',
//Ensure the label will always appear when overlaid on top of the item.
cssClass: 'darkText'
},
{
color: '#d24fa0'
},
{
color: '#8a32d7'
},
{
color: '#144bed'
},
{
color: '#479702'
},
{
color: '#72b403'
},
{
color: '#93c701'
},
{
color: '#ffd701'
},
{
color: '#f05514'
},
{
color: '#dc250e'
},
{
color: '#ba0808'
},
{
label: 'high',
color: 'black',
//Ensure the label will always appear when overlaid on top of the item.
cssClass: 'lightText'
}
]
},
//A simple legend that loads an image.
{
type: 'image',
subtitle: 'Image legend',
//URL to legend image.
url: 'https://i0.wp.com/gisgeography.com/wp-content/uploads/2016/05/Map-Example-Legend.png',
//Alt text should always be provided for legend images.
altText: 'A legend for the icons on the map.',
//Optionally specify a max width or height for the image.
maxHeight: 200,
footer: 'This legend was created using an image.'
},
//A simple HTML legend created using an HTML string.
{
type: 'html',
subtitle: 'Simple HTML legend',
html: '<span class="simple-legend">This <b>legend</b> is created using an <i>HTML</i> string.</span>'
},
//A gradient legend that uses multiple color stops, some having labels.
{
type: 'gradient',
subtitle: 'Gradient legend',
footer: 'A gradient legend that uses multiple color stops, some having labels.',
stops: [
{
offset: 0,
color: 'royalblue',
label: 'low'
},
{
offset: 0.25,
color: 'cyan'
},
{
offset: 0.5,
color: 'lime',
label: 'medium'
},
{
offset: 0.75,
color: 'yellow'
},{
offset: 1,
color: 'red',
label: 'high'
}
]
},
//A gradient legend that has transparency
{
type: 'gradient',
subtitle: 'Gradient legend - transparency',
footer: 'A gradient legend that has transparency.',
orientation: 'vertical',
barLength: 75,
stops: [
{
offset: 0,
color: 'rgba(0,0,0,0)',
label: 'Transparent'
},{
offset: 1,
color: 'black',
label: 'Solid'
}
]
},
//A gradient legend with stepped color stops.
{
type: 'gradient',
subtitle: 'Gradient legend - stepped',
footer: 'In this gradient each color has two stops, a starting stop and an end stop. The end stop has the same offset as the next colors starting stop.',
stops: [
{
offset: 0,
color: '#03939c',
label: '< -1'
}, {
offset: 0.167,
color: '#03939c'
}, {
offset: 0.167,
color: '#5ebabf'
}, {
offset: 0.334,
color: '#5ebabf'
}, {
offset: 0.334,
color: '#bae1e2'
}, {
offset: 0.501,
color: '#bae1e2'
}, {
offset: 0.501,
color: '#f8c0aa',
label: '0'
}, {
offset: 0.668,
color: '#f8c0aa'
}, {
offset: 0.668,
color: '#dd7755'
}, {
offset: 0.835,
color: '#dd7755'
}, {
offset: 0.835,
color: '#c22e00'
}, {
offset: 1,
color: '#c22e00',
label: '> 1'
},
]
}
],
//Optional resource key value pairs used to replace labels and titles with localized values specified in this object.
resx: {
'label1': 'Hello',
'label2': 'World'
}
});
//Add the legend control to the map.
map.controls.add(legend, {
position: 'bottom-left'
});
//Optionally add events to the legend control to know when the focused legend card changes.
map.events.add('legendfocused', legend, function(e){
console.log('legendfocused ' + e.legendIdx);
});
map.events.add('toggled', legend, function(e){
console.log('Minified ' + e.minimized);
});
});
}
</script>
<style>
.darkText {
color: black;
text-shadow: white 0 0 2px;
}
.lightText {
color: white;
text-shadow: black 0 0 2px;
}
</style>
</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 legend control</h1>
</legend>
This sample shows how to create a legend control and add it to the map.
There are several built in legend types, such as category, gradient, and image.
You can also create custom legend cards using HTML.
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>