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.
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
// Create labels for large cities TextSymbol* largecitiesTextSymbol = new TextSymbol(this); largecitiesTextSymbol->setFontFamily("Arial"); largecitiesTextSymbol->setFontWeight(FontWeight::Bold); largecitiesTextSymbol->setSize(16); largecitiesTextSymbol->setColor(QColor("blue")); largecitiesTextSymbol->setHaloColor(QColor("white")); largecitiesTextSymbol->setHaloWidth(2); // create arcade expression ArcadeLabelExpression* largeArcadeLabelExpression = new ArcadeLabelExpression("$feature.name", this); // create label definition and set where clause LabelDefinition* largeLabelDefinition = new LabelDefinition(largeArcadeLabelExpression, largecitiesTextSymbol, this); largeLabelDefinition->setWhereClause("[POPULATION] > 1250000"); // add label definition to the feature layer m_featureLayer->labelDefinitions()->append(largeLabelDefinition);
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.
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::setWhereClause())—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 (
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_, for example, may use an Arcade expression like this:
($feature.length_. A feature with a
ft * 0.3048) + ' meters'
343would display a label of
104.546 meters. See the Label expressions section for more details and examples.
Text symbol (
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::setMaxScale())—The scale range, defined with a minimum and maximum scale denominator, at which labels in the definition will be displayed. A value of
0for one of the range values means that the end of the range is not restricted.
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
LabelDefinition::setPriority())—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 (
LabelOverlapStrategy)—Control label overlap with geoelements and other labels.
Duplicate or repeating labels (
LabelDefinition::setMultipartStrategy())—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.
Depending upon your use case, you can define text for your labels using one of the following types of label expression classes:
ArcadeLabelExpression—An expression that uses the ArcGIS Arcade expression language to define label text.
SimpleLabelExpression—A simple label expression that uses the ArcGIS REST API labeling syntax.
WebmapLabelExpression—A web map script to be read and evaluated by a web map expression interpreter.
You can access feature attributes using Arcade's
$feature global variable. For example, to label roads with a
STREET_ field, you can refer to the value in that attribute with this syntax:
$feature.STREET_. 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.
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, '');
Simple expressions use ArcGIS REST API syntax. You can use functions in the expression to provide basic formatting of values, such as
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.
[NAME] CONCAT NEWLINE CONCAT \"(Pop=\" CONCAT [POPULATION] CONCAT \")\"
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:
Note that unlike a simple or Arcade expression, quotes are not needed around the literal text.
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:
<—Font name, size, and scaling.
<—Color (defined with RGB or CMYK values).
<—Bold / un-bold.
<—Italic / un-italic.
<—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.
"return \"<BOL><UND>\" + $feature.name + \"</UND></BOL>\" + TextFormatting.NewLine + \"(Population=\" + $feature.population + \")\";"
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.