Sometimes users may want to delete features from an online feature service.
How to use the sample
Tap on a feature to display a callout. Tap on the trash can icon in the callout to delete the feature.
How it works
Create and load an AGSServiceGeodatabase with a feature service URL.
Get the AGSServiceFeatureTable from the service geodatabase.
Create an AGSFeatureLayer from the service feature table.
Identify the selected feature by using AGSGeoView.identifyLayer(_:screenPoint:tolerance:returnPopupsOnly:maximumResults:completion:).
Remove the selected features from the AGSServiceFeatureTable using AGSFeatureTable.delete(_:completion:).
Update the data on the server using AGSServiceGeodatabase.applyEdits(completion:) on the AGSServiceGeodatabase, which will remove the feature from the online service.
Relevant API
AGSFeature
AGSFeatureLayer
AGSServiceFeatureTable
AGSServiceGeodatabase
Tags
deletion, feature, online, Service, table
Sample Code
DeleteFeaturesViewController.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
// Copyright 2016 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//// http://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 UIKit
import ArcGIS
classDeleteFeaturesViewController: UIViewController, AGSGeoViewTouchDelegate, AGSCalloutDelegate{
@IBOutletvar mapView: AGSMapView! {
didSet {
mapView.map =AGSMap(basemapStyle: .arcGISStreets)
// Set touch delegate on map view as self. mapView.touchDelegate =self mapView.callout.delegate =self }
}
/// The feature table to delete features from.var featureTable: AGSServiceFeatureTable!
/// The service geodatabase that contains damaged property features.var serviceGeodatabase: AGSServiceGeodatabase!
/// The feature layer created from the feature table.var featureLayer: AGSFeatureLayer!
/// Last identify operation.var lastQuery: AGSCancelable!
/// The currently selected feature.var selectedFeature: AGSFeature!
overridefuncviewDidLoad() {
super.viewDidLoad()
// Add the source code button item to the right of navigation bar. (navigationItem.rightBarButtonItem as?SourceCodeBarButtonItem)?.filenames = ["DeleteFeaturesViewController"]
// Load the service geodatabase.let damageFeatureService =URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer")! loadServiceGeodatabase(from: damageFeatureService)
}
/// Load and set a service geodatabase from a feature service URL./// - Parameter serviceURL: The URL to the feature service.funcloadServiceGeodatabase(fromserviceURL: URL) {
let serviceGeodatabase =AGSServiceGeodatabase(url: serviceURL)
serviceGeodatabase.load { [weakself] error inguardletself=selfelse { return }
iflet error = error {
self.presentAlert(error: error)
} else {
let featureTable = serviceGeodatabase.table(withLayerID: 0)!self.featureTable = featureTable
self.serviceGeodatabase = serviceGeodatabase
// Add the feature layer to the operational layers on map.let featureLayer =AGSFeatureLayer(featureTable: featureTable)
self.featureLayer = featureLayer
self.mapView.map?.operationalLayers.add(featureLayer)
self.mapView.setViewpoint(AGSViewpoint(center: AGSPoint(x: 544871.19, y: 6806138.66, spatialReference: .webMercator()), scale: 2e6))
}
}
}
funcshowCallout(forfeature: AGSFeature, attapLocation: AGSPoint) {
let title = feature.attributes["typdamage"] as!String mapView.callout.title = title
mapView.callout.accessoryButtonImage =UIImage(named: "Discard")
mapView.callout.show(for: feature, tapLocation: tapLocation, animated: true)
}
/// Delete a feature from the feature table.funcdeleteFeature(_feature: AGSFeature) {
featureTable.delete(feature) { [weakself] error iniflet error = error {
self?.presentAlert(message: "Error while deleting feature: \(error.localizedDescription)")
} else {
self?.applyEdits()
}
}
}
/// Apply local edits to the geodatabase.funcapplyEdits() {
guard serviceGeodatabase.hasLocalEdits() else { return }
serviceGeodatabase.applyEdits { [weakself] _, error iniflet error = error {
self?.presentAlert(message: "Error while applying edits: \(error.localizedDescription)")
}
}
}
// MARK: - AGSGeoViewTouchDelegatefuncgeoView(_geoView: AGSGeoView, didTapAtScreenPointscreenPoint: CGPoint, mapPoint: AGSPoint) {
iflet query = lastQuery { query.cancel() }
// Hide the callout. mapView.callout.dismiss()
lastQuery = mapView.identifyLayer(featureLayer, screenPoint: screenPoint, tolerance: 12, returnPopupsOnly: false) { [weakself] identifyLayerResult inguardletself=selfelse { return }
self.lastQuery =niliflet feature = identifyLayerResult.geoElements.first as?AGSFeature {
// Show callout for the feature.self.showCallout(for: feature, at: mapPoint)
// Update selected feature.self.selectedFeature = feature
} elseiflet error = identifyLayerResult.error {
self.presentAlert(error: error)
}
}
}
// MARK: - AGSCalloutDelegatefuncdidTapAccessoryButton(forcallout: AGSCallout) {
mapView.callout.dismiss()
let alertController =UIAlertController(title: "Are you sure you want to delete the feature?", message: nil, preferredStyle: .alert)
let alertAction =UIAlertAction(title: "Delete", style: .destructive) { [unownedself] _in deleteFeature(selectedFeature)
}
alertController.addAction(alertAction)
let cancelAlertAction =UIAlertAction(title: "Cancel", style: .cancel)
alertController.addAction(cancelAlertAction)
alertController.preferredAction = cancelAlertAction
present(alertController, animated: true)
}
}