Manage operational layers

View on GitHub

Add, remove, and reorder operational layers in a map.

Image of manage operational layers 1 Image of manage operational layers 2

Use case

Operational layers display the primary content of the map and usually provide dynamic content for the user to interact with (as opposed to basemap layers that provide context).

The order of operational layers in a map determines the visual hierarchy of layers in the view. You can bring attention to a specific layer by rendering above other layers.

How to use the sample

Tap the "Manage Layers" button to display the operational layers that are currently on the map. In the first section, tap the "-" button to remove a layer, or tap "Edit" to drag and reorder the layers. The map will be updated automatically.

The second section shows layers that have been removed from the map. Tap the "+" button to add a layer back to the map.

How it works

  1. Get the operational layers from the map's operationalLayers property.
  2. Add a layer using map.addOperationalLayer(newOperationalLayer:) or remove a layer using map.removeOperationalLayer(operationalLayer:). The last layer in the array will be rendered on top.

Relevant API

  • ArcGISMapImageLayer
  • Map

Additional information

You cannot add the same layer to the map multiple times or add the same layer to multiple maps. Instead, clone the layer with layer.clone() to create a new instance.

Tags

add, delete, layer, map, remove

Sample Code

ManageOperationalLayersView.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 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 SwiftUI
import ArcGIS

struct ManageOperationalLayersView: View {
    /// A map with a topographic basemap and centered on western USA.
    @State private var map = {
        let map = Map(basemapStyle: .arcGISTopographic)
        map.initialViewpoint = Viewpoint(
            center: Point(x: -133e5, y: 45e5, spatialReference: .webMercator),
            scale: 2e7
        )
        return map
    }()

    /// A Boolean value indicating whether to show the manage layers sheet.
    @State private var isShowingSheet = false

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

    var body: some View {
        MapView(map: map)
            .task {
                do {
                    // Add layers from urls.
                    let elevationImageLayer = ArcGISMapImageLayer(url: .worldElevations)
                    try await elevationImageLayer.load()

                    let censusTiledLayer = ArcGISMapImageLayer(url: .censusTiles)
                    try await censusTiledLayer.load()

                    map.addOperationalLayers([elevationImageLayer, censusTiledLayer])
                } catch {
                    self.error = error
                }
            }
            .toolbar {
                ToolbarItem(placement: .bottomBar) {
                    Button("Manage Layers") {
                        isShowingSheet = true
                    }
                    .sheet(isPresented: $isShowingSheet, detents: [.medium], dragIndicatorVisibility: .visible) {
                        ManageLayersSheetView(map: map)
                    }
                }
            }
            .errorAlert(presentingError: $error)
    }
}

struct ManageLayersSheetView: View {
    /// The map with the operational layers.
    let map: Map

    /// The action to dismiss the manage layers sheet.
    @Environment(\.dismiss) private var dismiss

    /// An array for all the layers currently on the map.
    @State private var operationalLayers: [Layer] = []

    /// An array for all the layers removed from the map.
    @State private var removedLayers: [Layer] = []

    var body: some View {
        VStack {
            ZStack {
                Text("Manage Layers")
                    .bold()
                HStack {
                    Spacer()
                    EditButton()
                }
            }
            .padding([.top, .leading, .trailing])

            List {
                Section {
                    ForEach(operationalLayers, id: \.id) { layer in
                        HStack {
                            Image(systemName: "minus.circle.fill")
                                .foregroundColor(.red)
                                .imageScale(.large)
                                .clipped()
                                .onTapGesture {
                                    // Remove layer from map on minus press.
                                    map.removeOperationalLayer(layer)
                                    removedLayers.append(layer)
                                    operationalLayers.removeAll(where: { $0.id == layer.id })
                                }
                            Text(layer.name)
                        }
                    }
                    .onMove { fromOffsets, toOffset in
                        // Reorder the map's operational layers on list row move.
                        operationalLayers.move(fromOffsets: fromOffsets, toOffset: toOffset)
                        map.removeAllOperationalLayers()
                        map.addOperationalLayers(operationalLayers)
                    }
                } header: {
                    Text("Operational Layers")
                        #if targetEnvironment(macCatalyst)
                        .padding(.top)
                        #endif
                } footer: {
                    Text("Tap \"Edit\" to reorder the layers.")
                }

                Section {
                    ForEach(removedLayers, id: \.id) { layer in
                        HStack {
                            Image(systemName: "plus.circle.fill")
                                .foregroundColor(.green)
                                .imageScale(.large)
                                .clipped()
                                .onTapGesture {
                                    // Add layer to map on plus press.
                                    map.addOperationalLayer(layer)
                                    operationalLayers.append(layer)
                                    removedLayers.removeAll(where: { $0.id == layer.id })
                                }
                            Text(layer.name)
                        }
                    }
                } header: {
                    Text("Removed Layers")
                }
            }
        }
        .background(Color(.systemGroupedBackground))
        .onAppear {
            operationalLayers = map.operationalLayers
        }
    }
}

private extension URL {
    /// A world elevations image layer URL.
    static var worldElevations: URL {
        URL(string: "https://sampleserver5.arcgisonline.com/arcgis/rest/services/Elevation/WorldElevations/MapServer")!
    }

    /// A census tiled layer URL.
    static var censusTiles: URL {
        URL(string: "https://sampleserver5.arcgisonline.com/arcgis/rest/services/Census/MapServer")!
    }
}

#Preview {
    NavigationView {
        ManageOperationalLayersView()
    }
}

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