Skip to content
View on GitHub

Create, update, and delete features to manage a feature layer.

Image of manage features sample

Use case

An end-user performing a survey may want to manage features on the map in various ways during the course of their work.

How to use the sample

Pick an operation, then tap on the map to perform the operation at that location. Available feature management operations include: "Create feature", "Delete feature", "Update attribute", and "Update geometry".

How it works

  1. Create a ServiceGeodatabase from a URL.
  2. Get a ServiceFeatureTable from the ServiceGeodatabase.
  3. Create a FeatureLayer derived from the ServiceFeatureTable instance.
  4. Apply the feature management operation upon selection.
  • Create feature: create a Feature with attributes and a location using the ServiceFeatureTable.
  • Delete feature: delete the selected Feature from the FeatureTable.
  • Update attribute: update the attribute of the selected Feature.
  • Update geometry: update the geometry of the selected Feature.
  1. Update the FeatureTable locally.
  2. Update the ServiceGeodatabase of the ServiceFeatureTable by calling applyEdits().
  • This pushes the changes to the server.

Relevant API

  • Feature
  • FeatureLayer
  • ServiceFeatureTable
  • ServiceGeodatabase

Additional information

When editing feature tables that are subject to database behavior (operations on one table affecting another table), it's now recommended to call these methods (apply or undo edits) on the ServiceGeodatabase object rather than on the ServiceFeatureTable object. Using the ServiceGeodatabase object to call these operations will prevent possible data inconsistencies and ensure transactional integrity so that all changes can be committed or rolled back.

Tags

amend, attribute, create, delete, deletion, details, edit, editing, feature, feature layer, feature table, geodatabase, information, moving, online service, service, update, updating, value

Sample Code

ManageFeaturesView.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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 // 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 ManageFeaturesView: View {  /// The data for the view.  @State private var data: Result<Data, Error>?   /// The result of the latest action.  @State private var status = ""   /// The screen location that the user tapped.  @State private var tapLocation: CGPoint?   /// The map location that the user tapped.  @State private var tapMapPoint: Point?   /// The placement of the callout.  @State private var calloutPlacement: CalloutPlacement?   /// The current viewpoint of the map view.  @State private var currentViewpoint: Viewpoint?   /// A Boolean value indicating whether the update attribute dialog is displayed.  @State private var isShowingUpdateAttributeDialog = false   var body: some View {  Group {  switch data {  case .success(let data):  // Show map view if data loads.  mapView(data)  case .failure:  // Show content unavailable if data does not load.  ContentUnavailableView(  "Error",  systemImage: "exclamationmark.triangle",  description: Text("Failed to load sample data.")  )  case .none:  // Show progress view during loading.  ProgressView()  }  }  .animation(.default, value: status)  .animation(.default, value: calloutPlacement)  .task { await loadData() }  }   @ViewBuilder  func mapView(_ data: Data) -> some View {  MapViewReader { mapView in  MapView(map: data.map)  .onSingleTapGesture { tapLocation, tapMapPoint in  if calloutPlacement == nil {  self.tapLocation = tapLocation  self.tapMapPoint = tapMapPoint  } else {  clearSelection()  }  }  .callout(placement: $calloutPlacement) { placement in  if let feature = placement.geoElement as? Feature {  featureCalloutContent(feature: feature, table: data.featureTable)  } else if let tapMapPoint {  addNewFeatureCalloutContent(table: data.featureTable, point: tapMapPoint)  }  }  .onNavigatingChanged { _ in  // Reset status when user moves the map.  clearStatus()  }  .onViewpointChanged(kind: .centerAndScale) { viewpoint in  // Track current viewpoint.  currentViewpoint = viewpoint  }  .overlay(alignment: .top) {  instructionsOverlay  }  .task(id: tapLocation) {  // Identify when we get a tap location.  guard let tapLocation, let tapMapPoint else { return }  if let identifyResult = try? await mapView.identify(on: data.featureLayer, screenPoint: tapLocation, tolerance: 12),  let geoElement = identifyResult.geoElements.first,  let feature = geoElement as? Feature {  // Place a callout for a feature.  calloutPlacement = .geoElement(geoElement)  data.featureLayer.selectFeature(feature)  } else {  // Place a callout for adding a new feature.  calloutPlacement = .location(tapMapPoint)  }  }  }  }   /// A callout that allows the user to add a new feature.  @ViewBuilder  func addNewFeatureCalloutContent(table: ServiceFeatureTable, point: Point) -> some View {  Button("Create Feature", systemImage: "plus.circle") {  clearSelection()  Task {  await createFeature(point: point)  }  }  .padding()  }   /// A callout that allows a user to modify or delete an existing feature.  @ViewBuilder  func featureCalloutContent(feature: Feature, table: ServiceFeatureTable) -> some View {  HStack {  VStack(alignment: .leading) {  Text("ID: \(feature.attributeValue(forKey: table.objectIDField) ?? "Unknown")")  Text("Damage: \(feature.damageKind?.rawValue ?? "Unknown")")  .font(.footnote)  .foregroundStyle(.secondary)  }  Menu {  Button("Update Attribute") {  isShowingUpdateAttributeDialog = true  }  Button("Update Geometry") {  // Hide callout, leave feature selected.  calloutPlacement = nil  Task {  await updateGeometry(  for: feature,  geometry: currentViewpoint?.targetGeometry  )  // Update callout location after moving feature.  calloutPlacement = .geoElement(feature)  }  }  Button("Delete Feature") {  Task {  clearSelection()  await delete(feature: feature)  }  }  } label: {  Label("Edit Feature", systemImage: "ellipsis")  .padding()  .contentShape(.rect)  .labelStyle(.iconOnly)  }  .fixedSize()  .confirmationDialog("Update Attribute", isPresented: $isShowingUpdateAttributeDialog) {  ForEach(DamageKind.allCases, id: \.self) { damageKind in  Button(damageKind.rawValue) {  isShowingUpdateAttributeDialog = false  clearSelection()   Task {  await updateAttribute(for: feature, damageKind: damageKind)  }  }  }  } message: {  Text("Choose a damage kind for the feature.")  }  }  .padding([.leading, .vertical])  }   /// Overlay with instructions for the user.  @ViewBuilder var instructionsOverlay: some View {  VStack(spacing: 8) {  Text("Tap the map to create a new feature, or tap an existing feature for more options.")  .multilineTextAlignment(.center)  if !status.isEmpty {  Text(status)  .font(.footnote)  .foregroundStyle(.secondary)  .multilineTextAlignment(.center)  }  }  .padding()  .frame(maxWidth: .infinity)  .background(.ultraThinMaterial)  }   /// Clears the selection on the feature layer, hides the callout, and  /// resets the status.  func clearSelection() {  if case .success(let data) = data {  data.featureLayer.clearSelection()  }  calloutPlacement = nil  clearStatus()  }   /// Clear the status.  func clearStatus() {  status = ""  } }  extension ManageFeaturesView {  /// Loads the data for this view.  func loadData() async {  let map = Map(basemapStyle: .arcGISStreets)  map.initialViewpoint = Viewpoint(  center: Point(x: -10_800_000, y: 4_500_000, spatialReference: .webMercator),  scale: 3e7  )   let geodatabase = ServiceGeodatabase(  url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0")!  )   do {  try await geodatabase.load()  let featureTable = geodatabase.table(withLayerID: 0)!  let layer = FeatureLayer(featureTable: featureTable)  map.addOperationalLayer(layer)  data = .success(  Data(map: map, geodatabase: geodatabase, featureTable: featureTable, featureLayer: layer)  )  } catch {  data = .failure(error)  }  }   /// Creates a new feature at a specified location and applies edits to the service.  /// - Parameters:  /// - point: The geometry for the feature you are creating.  func createFeature(point: Point) async {  guard case .success(let data) = data else { return }  let table = data.featureTable   let feature = table.makeFeature(  attributes: [  Feature.damageTypeFieldName: DamageKind.inaccessible.rawValue,  "primcause": "Earthquake"  ],  geometry: point  )  do {  try await table.add(feature)  _ = try await table.serviceGeodatabase?.applyEdits()  status = "Create feature succeeded."  } catch {  status = "Error creating feature."  }  }   /// Updates the attributes of a feature and applies edits to the service.  /// - Parameters:  /// - feature: The feature to update.  /// - damageKind: The kind used to set the feature's damage assessment attribute.  func updateAttribute(for feature: Feature, damageKind: DamageKind) async {  guard case .success(let data) = data else { return }  let table = data.featureTable   do {  feature.damageKind = damageKind  try await table.update(feature)  _ = try await table.serviceGeodatabase?.applyEdits()  status = "Update attribute succeeded."  } catch {  status = "Error updating attribute."  }  }   /// Updates the geometry of a feature and applies edits to the service.  /// This moves the feature to the center of the map.  /// - Parameters:  /// - feature: The feature to update.  /// - geometry: The new geometry.  func updateGeometry(for feature: Feature, geometry: Geometry?) async {  guard case .success(let data) = data else { return }  let table = data.featureTable   do {  feature.geometry = geometry  try await table.update(feature)  _ = try await table.serviceGeodatabase?.applyEdits()  status = "Update geometry succeeded."  } catch {  status = "Error updating geometry."  }  }   /// Deletes a feature from the table and applies edits to the service.  /// - Parameter feature: The feature to delete.  func delete(feature: Feature) async {  guard case .success(let data) = data else { return }  let table = data.featureTable   do {  try await table.delete(feature)  _ = try await table.serviceGeodatabase?.applyEdits()  status = "Delete feature succeeded."  } catch {  status = "Error deleting feature."  }  } }  extension ManageFeaturesView {  /// Data value for the sample.  struct Data {  /// The map that will be displayed.  let map: Map  /// The service geodatabase.  let geodatabase: ServiceGeodatabase  /// The service feature table.  let featureTable: ServiceFeatureTable  /// The feature layer.  let featureLayer: FeatureLayer  } }  extension ManageFeaturesView {  /// A value that describes the damage assessment value for a feature.  enum DamageKind: String, CaseIterable {  case inaccessible = "Inaccessible"  case affected = "Affected"  case minor = "Minor"  case major = "Major"  case destroyed = "Destroyed"  } }  extension Feature {  /// The name of the damage type field.  static let damageTypeFieldName = "typdamage"   /// The damage assessment of the feature.  var damageKind: ManageFeaturesView.DamageKind? {  get {  // Return the attribute value as a DamageKind.  ManageFeaturesView.DamageKind(  rawValue: attributeValue(forKey: Self.damageTypeFieldName) as? String ?? ""  )  } set {  // Set the attribute value on the feature by converting the  // DamageKind to a String value.  setAttributeValue(newValue?.rawValue, forKey: Self.damageTypeFieldName)  }  } }

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