Show labels on layers

View inQMLC++
View on GitHub
Sample viewer app

Display custom labels on a feature layer.

screenshot

Use case

Labeling features is useful to visually display a key piece of information or attribute of a feature on a map. For example, you may want to label rivers or street with their names.

How to use the sample

Pan and zoom around the United States. Labels for congressional districts will be shown in red for Republican districts and blue for Democrat districts. Notice how labels pop into view as you zoom in.

How it works

To show custom labels on a feature layer:

  1. Create a ServiceFeatureTable using a feature service URL.
  2. Create a FeatureLayer from the service feature table.
  3. Create an ArcadeLabelExpression for the label definition.

    • You can use fields of the feature by using $feature.field_name in the expression.
  4. Create a TextSymbol to control how the label text is styled.
  5. Create a LabelDefinition by passing in the ArcadeLabelExpression and TextSymbol.
  6. Add the definition to the feature layer with featureLayer.labelDefinitions().append(labelDefinition) .
  7. Lastly, enable labels on the layer using featureLayer.setLabelsEnabled().

Relevant API

  • ArcadeLabelExpression
  • FeatureLayer
  • LabelDefinition
  • TextSymbol

About the data

This sample uses the USA 116th Congressional Districts feature layer hosted on ArcGIS Online.

Additional information

Help regarding the Arcade label expression script for defining a label definition can be found on the ArcGIS Developers site.

Tags

attribute, deconfliction, label, labeling, string, symbol, text, visualization

Sample Code

ShowLabelsOnLayers.qmlShowLabelsOnLayers.cppShowLabelsOnLayers.h
                                
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
// [WriteFile Name=ShowLabelsOnLayers, Category=DisplayInformation]
// [Legal]
// Copyright 2018 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

import QtQuick 2.6
import QtQuick.Controls 2.2
import Esri.Samples 1.0

ShowLabelsOnLayersSample {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    // add a mapView component
    MapView {
        anchors.fill: parent
        objectName: "mapView"
    }
}

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