Geometry editing sample

Complexity: Advanced Data Requirement: ArcGIS.com

The purpose of this sample is to demonstrate how to create features (point, line, polygon) with the ArcGIS for Android API. The sample supports template based editing for the three types of feature layers (point, line and polygon). A list of available templates displays showing the templates' symbol to allow the user to quickly select the required feature to add to the map. When editing polygons or lines, the user can add and delete new vertexes and also move existing vertexes. Additional midpoint vertices are added automatically. The undo operation allows the user to remove one vertex at a time and a full undo history of vertex edits is maintained. A magnifier is provided via a long press on the map, which helps to move feature vertexes more accurately.

Sample design

The Activity class sets up the buttons and map layers in its onCreate() method. The map layers and their service endpoints are defined in the main.xml layout file. When the user clicks the Edit button, a MyTouchListener instance is created to handle the draw events and is added to the map. A ListView with all of the available feature templates for the map layers is then shown in a dialog box.

The MyTouchListener class extends the MapOnTouchListener that is provided in the MapView. This class captures the tap interaction of the user on the map and draws features on the map via a graphics layer.

To populate the feature template dialog box, all of the layers in the map are inspected and if an instance of an ArcGISFeatureLayer is found, the templates are retrieved. By passing the template to the featureLayer.createFeatureWithTemplate() method, the graphic and its symbol are returned, and with the template name, are added to the list for display.

If the user selects a feature template of type point, a point is added to the graphics layer. The user can perform additional clicks to move the point and a full history is maintained by an ArrayList of EditingStates classes (see the following). When the user clicks the Save button, the feature is saved to the server.

If the user selects a feature template of type polyline or polygon, the first time the user taps the map, a point is added to the user interface (UI) via the drawVertices() method. When there are more than one point, the points are connected using the drawPolyline() method. The logic for the construction of the polyline and polygon are similar. The only difference is that in one case, the geometry constructed is a multipath polyline versus a multipath polygon. A midpoint vertex is constructed in between every vertex pair by calculating the distance between two vertices. For all taps on the map, it is determined if the tap should be a new vertex or the selection of an existing vertex via a proximity test. If an existing vertex is selected (midpoint or user added vertex), it is highlighted and the next map touch event or long press moves that vertex rather than adding a new one.

The EditingStates class maintains a snapshot of all of the current points on the map (which points are selected and which point has just been inserted). An ArrayList of EditingStates classes is maintained in the activity—one for each edit operation—and this is used to perform the undo operation.

To save the feature to the server, the applyEdits method on the feature layer is used. There is no server communication while you are modifying the features geometry in the UI.

Using the sample

Steps:
  1. Load the sample on your device.
  2. Click the Edit button and choose a feature template.
  3. Click the map.
  4. If the selected feature template is a point, a point graphic is added at the selected location. The point can be moved by subsequent map clicks.
  5. If the selected feature template is a polyline or polygon, click until the features geometry is satisfactory. The user can click a vertex (black dot) or midpoint (green dot). To view the magnifier, click a point and hold (or long press).
  6. Use the relevant button to undo or redo your edits.
  7. Click Save to apply edits to the server.
1/24/2013