Sketch editor

View on GitHubSample viewer app

Use the Sketch Editor to edit or sketch a new point, line, or polygon geometry on to a map.

Image of sketch editor App

Use case

A field worker could annotate features of interest on a map (via the GUI) such as location of dwellings (marked as points), geological features (polylines), or areas of glaciation (polygons).

How to use the sample

Choose which geometry type to sketch from one of the available buttons along the bottom of the screen. Choose from points, multipoints, polylines, polygons, freehand polylines, and freehand polygons.

Use the buttons at the top of the screen to: undo or redo change made to the sketch whilst in sketch mode, and to save the sketch to the graphics overlay.

How it works

  1. Create a SketchEditor and pass it to the MapView with mapView.setSketchEditor(sketchEditor).
  2. Use SketchEditor.start(SketchCreationMode) to start sketching.
  • If editing an existing graphic's geometry, use SketchEditor.start(graphic.getGeometry).
  1. Check to see if undo and redo are possible during a sketch session using sketchEditor.canUndo() and sketchEditor.canRedo(). If it's possible, use sketchEditor.undo() and sketchEditor.redo().
  2. Check if sketch is valid using sketchEditor.isSketchValid(), then allow the sketch to be saved to a GraphicsOverlay.
  3. Get the geometry of the sketch using sketchEditor.getGeometry(), and create a new Graphic from that geometry. Add the graphic to the graphics overlay.
  4. To exit the sketch editor, use sketchEditor.stop().

Relevant API

  • Geometry
  • Graphic
  • GraphicsOverlay
  • MapView
  • SketchCreationMode
  • SketchEditor

Tags

draw, edit, sketch

Sample Code

MainActivity.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Copyright 2018 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.
 */

package com.esri.arcgisruntime.sample.sketcheditor;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.mapping.view.SketchCreationMode;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {

  private final String TAG = MainActivity.class.getSimpleName();

  private SimpleMarkerSymbol mPointSymbol;
  private SimpleLineSymbol mLineSymbol;
  private SimpleFillSymbol mFillSymbol;
  private MapView mMapView;
  private SketchEditor mSketchEditor;
  private GraphicsOverlay mGraphicsOverlay;

  private ImageButton mPointButton;
  private ImageButton mMultiPointButton;
  private ImageButton mPolylineButton;
  private ImageButton mPolygonButton;
  private ImageButton mFreehandLineButton;
  private ImageButton mFreehandPolygonButton;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // authentication with an API key or named user is required to access basemaps and other
    // location services
    ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);

    // define symbols
    mPointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.SQUARE, 0xFFFF0000, 20);
    mLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFFFF8800, 4);
    mFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.CROSS, 0x40FFA9A9, mLineSymbol);

    // inflate map view from layout
    mMapView = findViewById(R.id.mapView);
    // create a map with the Basemap Style topographic
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_LIGHT_GRAY);
    // set the map to be displayed in this view
    mMapView.setMap(map);
    mMapView.setViewpoint(new Viewpoint(34.056295, -117.195800, 100000));

    mGraphicsOverlay = new GraphicsOverlay();
    mMapView.getGraphicsOverlays().add(mGraphicsOverlay);

    // create a new sketch editor and add it to the map view
    mSketchEditor = new SketchEditor();
    mMapView.setSketchEditor(mSketchEditor);

    // get buttons from layouts
    mPointButton = findViewById(R.id.pointButton);
    mMultiPointButton = findViewById(R.id.pointsButton);
    mPolylineButton = findViewById(R.id.polylineButton);
    mPolygonButton = findViewById(R.id.polygonButton);
    mFreehandLineButton = findViewById(R.id.freehandLineButton);
    mFreehandPolygonButton = findViewById(R.id.freehandPolygonButton);

    // add click listeners
    mPointButton.setOnClickListener(view -> createModePoint());
    mMultiPointButton.setOnClickListener(view -> createModeMultipoint());
    mPolylineButton.setOnClickListener(view -> createModePolyline());
    mPolygonButton.setOnClickListener(view -> createModePolygon());
    mFreehandLineButton.setOnClickListener(view -> createModeFreehandLine());
    mFreehandPolygonButton.setOnClickListener(view -> createModeFreehandPolygon());
  }

  /**
   * When the point button is clicked, reset other buttons, show the point button as selected, and start point
   * drawing mode.
   */
  private void createModePoint() {
    resetButtons();
    mPointButton.setSelected(true);
    mSketchEditor.start(SketchCreationMode.POINT);
  }

  /**
   * When the multipoint button is clicked, reset other buttons, show the multipoint button as selected, and start
   * multipoint drawing mode.
   */
  private void createModeMultipoint() {
    resetButtons();
    mMultiPointButton.setSelected(true);
    mSketchEditor.start(SketchCreationMode.MULTIPOINT);
  }

  /**
   * When the polyline button is clicked, reset other buttons, show the polyline button as selected, and start
   * polyline drawing mode.
   */
  private void createModePolyline() {
    resetButtons();
    mPolylineButton.setSelected(true);
    mSketchEditor.start(SketchCreationMode.POLYLINE);

  }

  /**
   * When the polygon button is clicked, reset other buttons, show the polygon button as selected, and start polygon
   * drawing mode.
   */
  private void createModePolygon() {
    resetButtons();
    mPolygonButton.setSelected(true);
    mSketchEditor.start(SketchCreationMode.POLYGON);
  }

  /**
   * When the freehand line button is clicked, reset other buttons, show the freehand line button as selected, and
   * start freehand line drawing mode.
   */
  private void createModeFreehandLine() {
    resetButtons();
    mFreehandLineButton.setSelected(true);
    mSketchEditor.start(SketchCreationMode.FREEHAND_LINE);
  }

  /**
   * When the freehand polygon button is clicked, reset other buttons, show the freehand polygon button as selected,
   * and enable freehand polygon drawing mode.
   */
  private void createModeFreehandPolygon() {
    resetButtons();
    mFreehandPolygonButton.setSelected(true);
    mSketchEditor.start(SketchCreationMode.FREEHAND_POLYGON);
  }

  /**
   * When the undo button is clicked, undo the last event on the SketchEditor.
   */
  private void undo() {
    if (mSketchEditor.canUndo()) {
      mSketchEditor.undo();
    }
  }

  /**
   * When the redo button is clicked, redo the last undone event on the SketchEditor.
   */
  private void redo() {
    if (mSketchEditor.canRedo()) {
      mSketchEditor.redo();
    }
  }

  /**
   * When the stop button is clicked, check that sketch is valid. If so, get the geometry from the sketch, set its
   * symbol and add it to the graphics overlay.
   */
  private void stop() {
    if (!mSketchEditor.isSketchValid()) {
      reportNotValid();
      mSketchEditor.stop();
      resetButtons();
      return;
    }

    // get the geometry from sketch editor
    Geometry sketchGeometry = mSketchEditor.getGeometry();
    mSketchEditor.stop();
    resetButtons();

    if (sketchGeometry != null) {

      // create a graphic from the sketch editor geometry
      Graphic graphic = new Graphic(sketchGeometry);

      // assign a symbol based on geometry type
      if (graphic.getGeometry().getGeometryType() == GeometryType.POLYGON) {
        graphic.setSymbol(mFillSymbol);
      } else if (graphic.getGeometry().getGeometryType() == GeometryType.POLYLINE) {
        graphic.setSymbol(mLineSymbol);
      } else if (graphic.getGeometry().getGeometryType() == GeometryType.POINT ||
          graphic.getGeometry().getGeometryType() == GeometryType.MULTIPOINT) {
        graphic.setSymbol(mPointSymbol);
      }

      // add the graphic to the graphics overlay
      mGraphicsOverlay.getGraphics().add(graphic);
    }
  }

  /**
   * Called if sketch is invalid. Reports to user why the sketch was invalid.
   */
  private void reportNotValid() {
    String validIf;
    if (mSketchEditor.getSketchCreationMode() == SketchCreationMode.POINT) {
      validIf = "Point only valid if it contains an x & y coordinate.";
    } else if (mSketchEditor.getSketchCreationMode() == SketchCreationMode.MULTIPOINT) {
      validIf = "Multipoint only valid if it contains at least one vertex.";
    } else if (mSketchEditor.getSketchCreationMode() == SketchCreationMode.POLYLINE
        || mSketchEditor.getSketchCreationMode() == SketchCreationMode.FREEHAND_LINE) {
      validIf = "Polyline only valid if it contains at least one part of 2 or more vertices.";
    } else if (mSketchEditor.getSketchCreationMode() == SketchCreationMode.POLYGON
        || mSketchEditor.getSketchCreationMode() == SketchCreationMode.FREEHAND_POLYGON) {
      validIf = "Polygon only valid if it contains at least one part of 3 or more vertices which form a closed ring.";
    } else {
      validIf = "No sketch creation mode selected.";
    }
    String report = "Sketch geometry invalid:\n" + validIf;
    Snackbar reportSnackbar = Snackbar.make(findViewById(R.id.toolbarInclude), report, Snackbar.LENGTH_INDEFINITE);
    reportSnackbar.setAction("Dismiss", view -> reportSnackbar.dismiss());
    TextView snackbarTextView = reportSnackbar.getView().findViewById(com.google.android.material.R.id.snackbar_text);
    snackbarTextView.setSingleLine(false);
    reportSnackbar.show();
    Log.e(TAG, report);
  }

  /**
   * De-selects all buttons.
   */
  private void resetButtons() {
    mPointButton.setSelected(false);
    mMultiPointButton.setSelected(false);
    mPolylineButton.setSelected(false);
    mPolygonButton.setSelected(false);
    mFreehandLineButton.setSelected(false);
    mFreehandPolygonButton.setSelected(false);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.undo_redo_stop_menu, menu);
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.undo) {
      undo();
    } else if (id == R.id.redo) {
      redo();
    } else if (id == R.id.stop) {
      stop();
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  protected void onPause() {
    mMapView.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mMapView.dispose();
  }
}

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