Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Qt

Show labels on layers

Sample Viewer View Sample on GitHub

Display custom labels on a feature layer.

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. A FeatureLayer and ServiceFeatureTable are created by passing in a URL to the REST endpoint of a map service.
  2. A label JSON is created using the ArcGIS JSON specification for labels.
  3. A LabelDefinition is created from the label JSON.
  4. The LabelDefinition is added to the list of LabelDefinitions in the FeatureLayer instance.
  5. Labels are enabled on the FeatureLayer by setting labelsEnabled to true.

Relevant API

  • 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 JSON syntax for defining the LabelDefinition.FromJson syntax can be found in labeling info in the Web map specification.

Tags

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

Sample Code

import QtQuick 2.6
import Esri.ArcGISRuntime 100.9

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

    MapView {
        id: mapView
        anchors.fill: parent

        Map {
            BasemapLightGrayCanvas {}

            FeatureLayer {
                // Add a feature service
                ServiceFeatureTable {
                    url: "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_115th_Congressional_Districts/FeatureServer/0"
                }

                // Enable Labels
                labelsEnabled: true

                // Add a label definition to define labels for the layer
                //
                // Help regarding the JSON syntax for defining the LabelDefinition.FromJson syntax can be found here:
                // https://developers.arcgis.com/web-map-specification/objects/labelingInfo/
                // This particular JSON string will have the following characteristics:
                // (1) The 'labelExpressionInfo' defines that the label text displayed comes from the fields 'NAME',
                //     'PARTY' (R or D), and 'CDFIPS' in the feature service in the format
                //     "Firstname Lastname (R or D)
                //     District #".
                // (2) The 'labelPlacement' will be placed horizontally in the polygon.
                // (3) The 'where' clause restricts the labels to data from Republican or Democrat districts.
                // (4) The 'symbol' for the labeled text will be red (for Republican) or blue (for Democrat) with a white halo.
                LabelDefinition {
                    id: republicanJson
                    json: {
                        "labelExpressionInfo":{
                            "expression":"$feature.NAME + ' (' + left($feature.PARTY,1) + ') \\nDistrict ' + $feature.CDFIPS"
                        },
                        "labelPlacement":"esriServerPolygonPlacementAlwaysHorizontal",
                        "where":"PARTY = 'Republican'",
                        "symbol": {
                            "angle":0,
                            "backgroundColor":[0,0,0,0],
                            "borderLineColor":[0,0,0,0],
                            "borderLineSize":0,
                            "color":[255,0,0,255],
                            "font": {
                                "decoration":"none",
                                "size":8,
                                "style":"normal",
                                "weight":"normal"
                            },
                            "haloColor":[255,255,255,255],
                            "haloSize":1.5,
                            "horizontalAlignment":"center",
                            "kerning":false,
                            "type":"esriTS",
                            "verticalAlignment":"middle",
                            "xoffset":0,
                            "yoffset":0
                        }
                    }
                }

                LabelDefinition {
                    id: democratJson
                    json: {
                        "labelExpressionInfo":{
                            "expression":"$feature.NAME + ' (' + left($feature.PARTY,1) + ') \\nDistrict ' + $feature.CDFIPS"
                        },
                        "labelPlacement":"esriServerPolygonPlacementAlwaysHorizontal",
                        "where":"PARTY = 'Democrat'",
                        "symbol": {
                            "angle":0,
                            "backgroundColor":[0,0,0,0],
                            "borderLineColor":[0,0,0,0],
                            "borderLineSize":0,
                            "color":[0,0,255,255],
                            "font": {
                                "decoration":"none",
                                "size":8,
                                "style":"normal",
                                "weight":"normal"
                            },
                            "haloColor":[255,255,255,255],
                            "haloSize":1.5,
                            "horizontalAlignment":"center",
                            "kerning":false,
                            "type":"esriTS",
                            "verticalAlignment":"middle",
                            "xoffset":0,
                            "yoffset":0
                        }
                    }
                }

                // zoom to the layer once it loads
                onLoadStatusChanged: {
                    if (loadStatus === Enums.LoadStatusLoaded)
                        mapView.setViewpointCenterAndScale(fullExtent.center, 56759600);
                }
            }
        }
    }
}