Creating new features
This Topic explains the steps that are required to add new features to a feature layer using the ArcGIS API for Android. It includes a summary of the editing workflow along with more detailed information and code snippets to assist you in the creation of editing tools.
What do I need to add new features?
Creating new features involves adding new geometries to a Feature Layer. To do this there are various steps that need to be performed. Firstly you need to provide a tool with which the user can create features, this should create features of the correct geometry type for the layer. If this tool is interactive it will need to be connected to touch events on the MapView object, if this tool is to use the GPS location for creating or tracing geometries then it will need to integrate with the LocationService class. Once the geometry is created it can be styled according to the renderers defined in the feature layer. If the feature layer has subtypes or templates then these can be used to set the renderer and also set some default attributes. Once the attributes and renderer have been set the feature will need to be sent to the server using the feature layers applyEdits() method. More details on these steps can be found below.
Adding map touch events
For this it is necessary to add a listener on the MapView object to capture the location for new features and/or their vertices as the user touches the map. For points this could be a simple OnSingleTapListener, for polygons or lines this could be a MapOnTouchListener (for freehand style drawing) or a more complex OnSingleTapListener which has logic to record if a feature edit has started and finished (maybe via a Long click or double tap).
The new feature can be drawn into the graphics layer first, so the user can see the feature as it is being edited (see the Draw Graphics sample for more information).
If your Feature Layer has subtypes, templates or domains defined then it may be useful to display these choices to the user in an Android Spinner, Dialog or View. The default renderer and attributes from a subtype or template can be used for the feature being added saving the user from having to add this information. The ArcGISFeatureLayer class has some utility methods to help achieve this, see the createFeatureWithType() and createFeatureWithTemplate() methods. A code sample in the section below uses the createFeatureWithTemplate() method.
Applying the edits on the server
Once the feature has been captured it can be passed to the ArcGISFeatureLayer.applyEdits() method. This asynchronous method can be used to add new features, delete features and update existing features. Features are added as Graphic objects, which include geometry, attributes and a renderer. Added graphics are passed in as the first argument. The feature will be given a new ObjectId on the server so this field is not required for new features. The following method shows this:
/**
* Adds a new feature using the defined Template;
*/
public void applyAddFeature(Geometry geometry, FeatureTemplate template, ArcGISFeatureLayer featureLayer) {
//create a graphic using the template
Graphic graphic = featureLayer.createFeatureWithTemplate(template, geometry);
featureLayer.applyEdits(new Graphic[] { graphic }, null, null, new CallbackListener<FeatureEditResult[][]>() {
public void onError(Throwable error) {
// TODO implement error code
}
public void onCallback(FeatureEditResult[][] editResult) {
// Check the response for success or failure
if (editResult[0] != null && editResult[0][0] != null && editResult[0][0].isSuccess()) {
// TODO implement success logic
}
});
}
A new CallbackListener needs to be created to handle the asynchronous response from the applyEdits() method. The onCallback() method receives a FeatureEditResult[][] multi-dimensional array object. This result object contains FeatureEditResult objects for every add, delete and update. In the code above, editResult[0] would provide add results, editResult[1] delete results and editResult[2] update results. A FeatureEditResult is returned for every feature edit, in the case above we are only expecting 1 result which would be referenced by the following : editResult[0][0].
The onCallback() method gets called even if there is an error with the edits, so you need to add some code like the above to check for success or failure. The onError() method will also fire after this method if there has been an error.