Set visibility of subtype sublayer

View on GitHub

Display a composite layer of all the subtype values in a feature class.

Image of Set visibility of subtype sublayer sample

Use case

This is useful for controlling labeling, visibility, and symbology of a given subtype as though they are distinct layers on the map.

How to use the sample

The sample loads with the sublayer visible on the map. Toggle its visibility by tapping the first switch. Toggle between the sublayer's original renderer and an alternate renderer using the second switch. Tap the "Set Current to Minimum Scale" button to set the sublayer's minimum scale to the current map scale.

How it works

  1. Create a SubtypeFeatureLayer from a ServiceFeatureTable that defines a subtype, and add it to the Map.
  2. Get a SubtypeSublayer from the subtype feature layer using its name.
  3. Enable the sublayer's labels and define them with LabelDefinition.
    • Use SimpleLabelExpression to set the expression for label definitions.
  4. Make a switch to toggle the sublayer's visibility.
  5. Create an alternate renderer by making a SimpleRenderer.
  6. Get the current map scale and make it the minimum map scale.

Relevant API

  • LabelDefinition
  • ServiceFeatureTable
  • SimpleLabelExpression
  • SubtypeFeatureLayer
  • SubtypeSublayer

About the data

The feature service layer in this sample represents an electric network in Naperville, Illinois, which contains a utility network with asset classification for different devices.

Additional information

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

Tags

asset group, feature layer, labeling, sublayer, subtype, symbology, utility network, visible scale range

Sample Code

SetVisibilityOfSubtypeSublayerView.swiftSetVisibilityOfSubtypeSublayerView.swiftSetVisibilityOfSubtypeSublayerView.Model.swiftSetVisibilityOfSubtypeSublayerView.Views.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
// Copyright 2023 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 SetVisibilityOfSubtypeSublayerView: View {
    /// The view model for the sample.
    @StateObject private var model = Model()

    /// A Boolean value indicating whether the settings should be presented.
    @State private var isShowingSettings = false

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

    var body: some View {
        MapView(map: model.map)
            .onScaleChanged { scale in
                model.currentScale = scale
                model.formatCurrentScaleText()
            }
            .overlay(alignment: .top) {
                Text("Current scale: \(model.currentScaleText)")
                    .frame(maxWidth: .infinity, alignment: .center)
                    .padding(8)
                    .background(.ultraThinMaterial, ignoresSafeAreaEdges: .horizontal)
                    .multilineTextAlignment(.center)
            }
            .toolbar {
                ToolbarItem(placement: .bottomBar) {
                    Button("Visibility Settings") {
                        isShowingSettings.toggle()
                    }
                    .popover(isPresented: $isShowingSettings) {
                        NavigationStack {
                            SettingsView(model: model)
                        }
                        .presentationDetents([.fraction(0.5)])
                        .frame(idealWidth: 320, idealHeight: 340)
                    }
                }
            }
            .task {
                do {
                    try await model.setup()
                } catch {
                    // Presents an error message if the subtype layer fails to load.
                    self.error = error
                }
            }
            .errorAlert(presentingError: $error)
    }
}

#Preview {
    NavigationStack {
        SetVisibilityOfSubtypeSublayerView()
    }
}

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