Add features (feature service)

View on GitHub
Sample viewer app

Add features to a feature layer.

Add features (feature service) sample

Use case

An end-user performing a survey may want to add features to the map during the course of their work.

How to use the sample

Tap on a location on the map to add a feature at that location.

How it works

An AGSFeature instance is added to an AGSServiceFeatureTable which then pushes that new feature to the server.

  1. Create an AGSServiceFeatureTable from a URL.
  2. Create an AGSFeatureLayer derived from the AGSServiceFeatureTable instance.
  3. Create an AGSFeature with attributes and a location using the AGSServiceFeatureTable.
  4. Add the AGSFeature to the AGSServiceFeatureTable.
  5. Use AGSServiceFeatureTable.applyEdits(completion:) to apply edits to the AGSServiceFeatureTable, which will upload the new feature to the online service.

Relevant API

  • AGSFeature
  • AGSFeatureEditResult
  • AGSFeatureLayer
  • AGSServiceFeatureTable

Tags

edit, feature, online service

Sample Code

AddFeaturesViewController.swift
                                                                                                
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
// 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

class AddFeaturesViewController: UIViewController, AGSGeoViewTouchDelegate {
    @IBOutlet private var mapView: AGSMapView!

    private var featureTable: AGSServiceFeatureTable!
    private var lastQuery: AGSCancelable!

    override func viewDidLoad() {
        super.viewDidLoad()

        // add the source code button item to the right of navigation bar
        (self.navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["AddFeaturesViewController"]

        // assign the map to the map view
        let map = AGSMap(basemapStyle: .arcGISStreets)
        self.mapView.map = map
        self.mapView.setViewpoint(AGSViewpoint(center: AGSPoint(x: 544871.19, y: 6806138.66, spatialReference: .webMercator()), scale: 2e6))
        // set touch delegate on map view as self
        self.mapView.touchDelegate = self

        // instantiate service feature table using the url to the service
        self.featureTable = AGSServiceFeatureTable(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0")!)
        // create a feature layer using the service feature table
        let featureLayer = AGSFeatureLayer(featureTable: self.featureTable)

        // add the feature layer to the operational layers on map
        map.operationalLayers.add(featureLayer)
    }

    func addFeature(at mappoint: AGSPoint) {
        // disable interaction with map view
        mapView.isUserInteractionEnabled = false

        // normalize geometry
        let normalizedGeometry = AGSGeometryEngine.normalizeCentralMeridian(of: mappoint)!

        // attributes for the new feature
        let featureAttributes = ["typdamage": "Minor", "primcause": "Earthquake"]
        // create a new feature
        let feature = featureTable.createFeature(attributes: featureAttributes, geometry: normalizedGeometry)

        // show the progress hud
        UIApplication.shared.showProgressHUD(message: "Adding..")

        // add the feature to the feature table
        featureTable.add(feature) { [weak self] (error: Error?) in
            UIApplication.shared.hideProgressHUD()

            if let error = error {
                self?.presentAlert(message: "Error while adding feature: \(error.localizedDescription)")
            } else {
                // applied edits on success
                self?.applyEdits()
            }
            // enable interaction with map view
            self?.mapView.isUserInteractionEnabled = true
        }
    }

    func applyEdits() {
        self.featureTable.applyEdits { [weak self] (featureEditResults: [AGSFeatureEditResult]?, error: Error?) in
            if let error = error {
                self?.presentAlert(message: "Error while applying edits :: \(error.localizedDescription)")
            } else {
                if let featureEditResults = featureEditResults,
                    featureEditResults.first?.completedWithErrors == false {
                    self?.presentAlert(message: "Edits applied successfully")
                }
                UIApplication.shared.hideProgressHUD()
            }
        }
    }

    // MARK: - AGSGeoViewTouchDelegate

    func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        // add a feature at the tapped location
        self.addFeature(at: mapPoint)
    }
}

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