Skip To Content

Edit FeatureLayer

In this topic

Editing features

XWhen the feature layer displays features, with an editable ArcGIS feature service, you can make changes to those features, delete them, or add new features to the service. It is possible that the server administrator may have disabled the editing capability of the feature service so make sure that the layer is editable before making any edits. You can determine if a layer is editable by looking for edit operations on the Services Directory page for the feature service layer.

Feature layer operations

You can also programmatically check if a layer is editable by inspecting the editable property as follows:

if !featureLayer.editable {
 println("Layer does not support editing")
 //bail out
}

To modify an existing feature, you need a reference to the corresponding AGSGraphic object. You can get the graphic in a variety of ways, for example, in response to a user clicking the map, or by iterating through the graphics collection of the layer. If you know the ObjectID of the feature you're looking for, you can find its corresponding graphic by using the convenience lookupFeatureWithObjectId: method.

Once you have a reference to the graphic, you can make changes to its geometry or attributes. These changes are only applied on the computer and the service remains unmodified. Call updateFeatures: when you are ready to post your changes to the service.

//modify the graphic
let graphic = AGSGraphic()
        
if featureLayer.canUpdateFeature(graphic) {
 graphic.geometry = newGeometry
 graphic.setAttribute(value: "<new-value>", forKey: "<field-name>")
 ...
            
 //update the service 
 featureLayer.updateFeatures([graphic])
}

The feature layer informs its editingDelegate when edit operations succeed or encounter errors. Set one of your classes as the feature layer's editingDelegate to receive results of successful operations and to handle errors.

To delete features, use deleteFeaturesWithObjectIds:passing in an array of ObjectIDs of features that need to be deleted, or you can delete features based on attribute or spatial relationships using deleteFeaturesWithWhereClause:geometry:spatialRelation:. In both cases, features are removed from the layer and service.

featureLayer.deleteFeaturesWithWhereClause("<field-name> = <value>", geometry: nil, spatialRelation: .Contains)

To add a new feature, construct a graphic for it. To construct a graphic, specify a feature type or feature template.

Features in a layer can be of many types. For example, schools can be of type public or private. Each type may display with a different symbol on the map. To create a new school feature, specify its type (if it should be a public school or a private school).

A feature template provides default settings for a feature, for example, its attribute values. Templates make it easy to create new features. A layer can have multiple templates or if the layer has feature types, each type can have one or more templates. Feature types and feature templates are defined by the service, and are available on the layer through the types and templates properties.

let privateSchool = featureLayer.types[index] as AGSFeatureType
let newSchool = featureLayer.featureWithType(privateSchool)

Even when you have constructed the graphic, note that it is still not added to the layer and won't display on the map. Call addFeatures: if you are ready to add the feature to the service. When this operation succeeds, the graphic is automatically added to the feature layer and displays on the map.

featureLayer.addFeatures([newSchool])

If you do not want to immediately persist the graphic to the feature service, manually add it to the feature layer's graphics collection by using addGraphic:. If the graphic has a valid geometry, it is displayed by the map. You can later call addFeatures: to persist the graphic to the service. If you decide not to persist the graphic, manually remove it from the feature layer by using removeGraphic:.

Each add, delete, and update operation is performed over a separate network request. If you are performing a large number of edits, it is expensive to perform them as separate operations. When possible, it is more efficient to perform all edits in a batch over a single network request using applyEditsWithFeaturesToAdd:toUpdate:toDelete:.