Skip to content

Show labels on layer in 3D

View on GitHub

Display custom labels in a 3D scene.

Image of Show labels on layer in 3D sample

Use case

Labeling features is useful to visually display information or attributes on a scene. For example, city officials or maintenance crews may want to show installation dates of features of a gas network.

How to use the sample

Pan and zoom to explore the scene. Notice the labels showing installation dates of features in the 3D gas network.

How it works

  1. Create a scene from a PortalItem.
  2. Add the scene to a SceneView and load it.
  3. After loading is complete, obtain the FeatureLayer from one of the GroupLayers in the scene's operational layers.
  4. Create a TextSymbol to use for displaying the label text.
  5. Create a LabelDefinition using an ArcadeLabelExpression.
  6. Add the definition to the feature layer with featureLayer.addLabelDefinition(labelDefinition).
  7. Lastly, enable labels on the layer using featureLayer.labelsAreEnabled.

Relevant API

  • ArcadeLabelExpression
  • FeatureLayer
  • LabelDefinition
  • Scene
  • SceneView
  • TextSymbol

About the data

This sample shows a New York City infrastructure scene hosted on ArcGIS Online.

Tags

3D, arcade, attribute, buildings, label, model, scene, symbol, text, URL, visualization

Sample Code

ShowLabelsOnLayerIn3DView.swift
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
// Copyright 2025 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
//
//   https://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.

import ArcGIS
import SwiftUI

struct ShowLabelsOnLayerIn3DView: View {
    /// A scene with a scene layer of utilities infrastructure in New York City.
    @State private var scene: ArcGIS.Scene = Scene(
        item: PortalItem(
            portal: .arcGISOnline(connection: .anonymous),
            id: .newYorkCityInfrastructure
        )
    )

    /// The gas network feature layer on the scene.
    private var gasFeatureLayer: FeatureLayer {
        let groupLayer = scene.operationalLayers.first(where: { $0.name == "Gas" }) as! GroupLayer
        return groupLayer.layers.first(where: { $0.name == "Gas Main" }) as! FeatureLayer
    }

    /// The error shown in the error alert.
    @State private var error: Error?

    var body: some View {
        SceneView(scene: scene)
            .task {
                do {
                    try await scene.load()
                    addLabels(to: gasFeatureLayer)
                } catch {
                    self.error = error
                }
            }
            .errorAlert(presentingError: $error)
    }
}

private extension ShowLabelsOnLayerIn3DView {
    /// Adds labels to a feature layer.
    /// - Parameter layer: The `FeatureLayer` to add the labels to.
    func addLabels(to layer: FeatureLayer) {
        // Create a label definition.
        let labelDefinition = makeLabelDefinition()

        // Add label definition to the layer.
        layer.addLabelDefinition(labelDefinition)

        // Turn on labeling.
        layer.labelsAreEnabled = true
    }

    /// Creates a label definition
    func makeLabelDefinition() -> LabelDefinition {
        // The styling for the label.
        let textSymbol = TextSymbol(color: .orange, size: 16)
        textSymbol.haloColor = .white
        textSymbol.haloWidth = 2

        // Make an arcade label expression.
        let arcadeLabelExpression = ArcadeLabelExpression(arcadeString: "Text($feature.INSTALLATIONDATE, `DD MMM YY`)")
        let labelDefinition = LabelDefinition(labelExpression: arcadeLabelExpression, textSymbol: textSymbol)
        labelDefinition.placement = .lineAboveAlong
        labelDefinition.usesCodedValues = true

        return labelDefinition
    }
}

private extension PortalItem.ID {
    /// The ID to the "New York City infrastructure with 3D labels" web scene portal item on ArcGIS Online.
    static var newYorkCityInfrastructure: Self { Self("850dfee7d30f4d9da0ebca34a533c169")! }
}

#Preview {
    ShowLabelsOnLayerIn3DView()
}

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