Add labels

Feature layers, graphics overlays, and map image layer sublayers in your app can be labeled using a combination of attribute values, text strings, and values calculated with an expression. You can determine how labels are positioned and prioritized, and how conflicts between overlapping labels are automatically and dynamically resolved.

For feature layers, graphics overlays, and map image sublayers, labeling is implemented using a collection of LabelDefinition objects to define things like:

  • What labels look like (font, size, color, angle, and so on).
  • At which scales they should be visible.
  • The text to display (perhaps using attribute values).
  • Which geoelements should be labeled.
  • How to handle overlaps and prioritize labels.

If you want to label everything in your layer or overlay the same way, you can define a single label definition. If you want to display different labels for different attribute values, you can add as many label definitions as you need to define distinct sets of geoelements for labeling.

Define labels

Label definitions establish the label text and how labels are rendered for a specified group of geoelements using attributes, which provide the label text as well as determine which label is applied to each geoelement. The map in the following image uses three label definitions: one with a large blue label for cities with populations over 1.5 million, smaller black labels for cities between 0.5 and 1.5 million, and a small gray label for cities under 0.5 million. For the label text, each definition uses the value from the name attribute.

Label definitions using population
Use dark colors for code blocksCopy
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
        val labelDefSmall = LabelDefinition(arcadeExpressionSmall, textSymbolSmallCities).apply {
            whereClause = "[POPULATION] < 500000"
            placement = LabelingPlacement.PointBelowCenter
        }

        // Add the label definitions to the city layer's label definitions collection.
        citiesLayer.labelDefinitions.addAll(
            listOf(labelDefLarge, labelDefMedium, labelDefSmall)
        )

        // Make sure to enable labels for the layer so they appear.
        citiesLayer.labelsEnabled = true

Each label's symbol, text, placement, overlap rules, and so on is represented with a LabelDefinition and is constructed to conform to label properties defined in the Web map specification for labels. In order to provide a more accurate representation of labels created using ArcGIS Pro (for a mobile map package, for example), additional label properties are defined that are not included in the web map specification. These properties provide additional control for things like the layout, orientation, and stacking of label text and label overlap behavior. See the LabelDefinition class for details.

Most properties for a label definition are optional and will use a default value if not assigned explicitly. At a minimum, you must provide an expression that defines the label text and a symbol (with at least color, font size, and type) to display labels for a layer. Refer to the label properties section for a description of some common properties available for label definitions.

Label properties

The following list describes some common aspects of label display and behavior that you can control in a label definition. Relevant classes and members are shown in parentheses.

  • Which geoelements to label (LabelDefinition.whereClause)—The geoelements labeled with the label definition are determined by evaluating an attribute expression. This expression is defined using SQL syntax, Arcade expressions are not supported for defining a label definition's where clause. If this expression is not defined, all elements are included in the definition. When using multiple label definitions, it's important that expressions uniquely assign geoelements for each definition. Creating multiple label definitions for a single layer is useful when you want to distinguish labels for certain types of elements. When labeling a cities layer, for example, you may want to distinguish capital cities from the others by using a larger font or different color.

  • Label text (LabelDefinition.expression, SimpleLabelExpression, ArcadeLabelExpression, WebmapLabelExpression)—A label expression can be used to determine the text to display for each geoelement in the label definition. The label text can come from a combination of available attributes, text strings, and expressions. A label showing length in meters from values in feet from an attribute named length_ft, for example, may use an Arcade expression like this: ($feature.length_ft * 0.3048) + ' meters'. A feature with a length_ft value of 343 would display a label of 104.546 meters. See the Label expressions section for more details and examples.

  • Text symbol (LabelDefinition.textSymbol, TextSymbol)—The font, size, color, angle, and so on, used to display labels in the definition. You can also provide a background color or halo for the text symbol.

  • Display scale range (LabelDefinition.minScale, LabelDefinition.maxScale)—The scale range, defined with a minimum and maximum scale denominator, at which labels in the definition will be displayed. A value of 0 for one of the range values means that the end of the range is not restricted.

  • Placement (LabelDefinition.placement, LabelingPlacement)—Labels can be placed at a specified position relative to the elements they describe. There are different options for placement depending upon the geometry of the geoelements being labeled. Line features, for example, may have labels placed above the center of the line, below the center of the line, above the end point, and so on. The default is automatic. Depending upon the type of geometry, this means:

    • Above right for point labels
    • Above along for line labels
    • Always horizontal for polygon labels
  • Priority (LabelDefinition.priority)—A label definition can be given a priority relative to other definitions in the layer. If labels from different definitions conflict in their placement, the label from the highest-priority definition will be displayed. Values indicate priority relative to other label definitions, with lower values indicating higher priority. If priority is not specified, a default prioritization is used based on geometry type: points (top priority), followed by lines, and then polygons.

  • Label overlap (LabelDefinition.deconflictionStrategy, LabelDefinition.labelOverlapStrategy, LabelDeconflictionStrategy, LabelOverlapStrategy)—Control label overlap with geoelements and other labels.

  • Duplicate or repeating labels (LabelDefinition.removeDuplicatesStrategy, LabelDefinition.repeatStrategy, LabelDefinition.multipartStrategy)—You can define preferences for removing duplicate labels within a specified radius as well as for repeating labels along lines at a constant interval. You can also control how geoelements with multipart geometry should be labeled.

Label display can also be affected by applying a reference scale to the map. Reference scale is the scale at which symbols and labels appear at their intended, true size. As you zoom in and out beyond the reference scale, symbols and text will increase or decrease in size relative to the reference scale to keep a consistent size on the map. If no reference scale is set, symbols remain a constant size on the screen and do not change size as you zoom in or out. Individual layers in a map can opt in or out of honoring the map's reference scale.

Label expressions

Depending upon your use case, you can define text for your labels using one of the following types of label expression classes:

Arcade expressions

Arcade is a simple, lightweight scripting language that can evaluate expressions at runtime. It was designed specifically for creating custom visualizations and labeling expressions in ArcGIS. Users can write, share, and execute custom expressions in ArcGIS Pro, ArcGIS Online, ArcGIS Maps SDK for JavaScript, and ArcGIS Maps SDKs for Native Apps.

You can access feature attributes using Arcade's $feature global variable. For example, to label roads with a STREET_NAME field, you can refer to the value in that attribute with this syntax: $feature.STREET_NAME. Arcade also provides built-in functions for mathematical calculations, logical operations, and formatting that you can use in your expression. Consult the Arcade guide for details and usage examples.

When defining a label, the final line of the expression must evaluate to a string or a number.

The following Arcade expression reads the attribute values for wind speed and direction. It then uses conditional logic (When function) to assign a cardinal direction based on azimuth. Another conditional function (IIf) is used to return an empty string for wind speeds of 0 and a formatted label for all other values.

Use dark colors for code blocksCopy
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
        // Arcade expression to show wind speed and cardinal direction.
        // The escape sequence \$ is required because $ is a special character n.
        // The resulting code passed to the Arcade interpreter will be:
        // var DEG = $feature.WIND_DIRECT; var SPEED = $feature.WIND_SPEED;  etc...);
        val windExpression =
            "var DEG = \$feature.WIND_DIRECT;" +
                    "var SPEED = \$feature.WIND_SPEED; " +
                    "var DIR = When(SPEED == 0, '', " +
                    "(DEG < 22.5 && DEG >= 0) || DEG > 337.5, 'N', " +
                    "DEG >= 22.5 && DEG < 67.5, 'NE', " +
                    "DEG >= 67.5 && DEG < 112.5, 'E', " +
                    "DEG >= 112.5 && DEG < 157.5, 'SE', " +
                    "DEG >= 157.5 && DEG < 202.5, 'S', " +
                    "DEG >= 202.5 && DEG < 247.5, 'SW', " +
                    "DEG >= 247.5 && DEG < 292.5, 'W', " +
                    "DEG >= 292.5 && DEG < 337.5, 'NW', ''); " +
                    "return IIf(SPEED > 0, SPEED + ' mph ' + DIR, ''); "

        // Create Arcade expressions to label low/moderate/high wind speeds.
        // (Note: although these use the same expression text, they must be separate expression objects.)
        val arcadeExpressionLow = ArcadeLabelExpression(windExpression)
        val arcadeExpressionModerate = ArcadeLabelExpression(windExpression)
        val arcadeExpressionHigh = ArcadeLabelExpression(windExpression)

        // Create three label definitions for different wind speeds.
        val lowWindLabelDef = LabelDefinition(arcadeExpressionLow, textSymbolLow).apply {
            whereClause = "[WIND_SPEED] <= 10"
            placement = LabelingPlacement.PointBelowCenter
        }
Labels of wind speed and direction

Simple expressions

Simple expressions use ArcGIS REST API syntax. You can use functions in the expression to provide basic formatting of values, such as ROUND(), CONCAT, and FORMATDATETIME(). See JSON labeling objects for more information.

The simple expression below creates a label that combines static text, city name, and population. Note that the static text must be inside quotes.

Use dark colors for code blocksCopy
1
[NAME] CONCAT NEWLINE CONCAT \"(Pop=\" CONCAT [POPULATION] CONCAT \")\"
Label showing city name and population

Web map expressions

A WebmapLabelExpression expression provides a legal web map script to be read and evaluated by a web map expression interpreter.

A web map label expression that combines static text with an attribute value will look like this: State {State_Name}.

Note that unlike a simple or Arcade expression, quotes are not needed around the literal text.

Use text formatting tags

Text used for labels can contain some of the same formatting tags used by ArcGIS Pro. These tags are similar to HTML tags and are used to modify the appearance of the text. The following tags are supported:

  • <FNT>—Font name, size, and scaling.
  • <CLR>—Color (defined with RGB or CMYK values).
  • <BOL>, <_BOL>—Bold / un-bold.
  • <ITA>, <_ITA>—Italic / un-italic.
  • <UND>, <_UND>—Underline / un-underline.

These tags allow you to format parts of the text differently than what has been defined for the label's text symbol. The following expression, for example, displays the name attribute with bold, underlined text. The rest of the text uses the unaltered text symbol previously defined for the label definition.

Use dark colors for code blocksCopy
1
2
"return \"<BOL><UND>\" + $feature.name + \"</UND></BOL>\" + TextFormatting.NewLine +
         \"(Population=\" + $feature.population + \")\";"
Name and population label with additional formatting for the name

See the Text formatting tags topic in the ArcGIS Pro documentation for more information about these formatting tags and how they can be used with your labels.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.