Sketch on map

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 on map

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. Choose from points, multipoints, polylines, polygons, freehand polylines, freehand polygons and rectangles.

Use the control panel to cancel the sketch, undo or redo changes made to the sketch and to save the sketch to the graphics overlay. There is also the option to select a saved graphic and edit its geometry using the Sketch Editor. The graphics overlay can be cleared using the clear all button.

How it works

  1. Create a SketchEditor and set it to the MapView with mapView.setSketchEditor(sketchEditor).
  2. Use SketchEditor.start(SketchCreationMode.chooseGeometryType) to start sketching. If editing an existing graphic's geometry, use SketchEditor.start(graphic.getGeometry).
  3. 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().
  4. Check if sketch is valid using sketchEditor.isSketchValid(), then allow the sketch to be saved to a GraphicsOverlay.
  5. Get the geometry of the sketch using sketchEditor.getGeometry(), and create a new Graphic from that geometry. Add the graphic to the graphics overlay.
  6. To exit the sketch editor, use sketchEditor.stop().

Relevant API

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

Tags

draw, edit

Sample Code

SketchOnMapController.javaSketchOnMapController.javaSketchOnMapSample.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * 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.samples.sketch_on_map;

import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.control.Button;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Geometry;
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.IdentifyGraphicsOverlayResult;
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;

public class SketchOnMapController {

  @FXML
  private MapView mapView;
  @FXML
  private Button redoButton;
  @FXML
  private Button undoButton;
  @FXML
  private Button clearButton;
  @FXML
  private Button saveButton;
  @FXML
  private Button editButton;
  @FXML
  private Button stopButton;

  private SketchEditor sketchEditor;
  private GraphicsOverlay graphicsOverlay;
  private Graphic graphic;
  private SimpleFillSymbol fillSymbol;
  private SimpleLineSymbol lineSymbol;
  private SimpleMarkerSymbol pointSymbol;

  public void initialize() {

    // authentication with an API key or named user is required to access basemaps and other location services
    String yourAPIKey = System.getProperty("apiKey");
    ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);

    // create a map with the standard imagery basemap style
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_IMAGERY_STANDARD);

    // set the map to the map view
    mapView.setMap(map);

    // set a viewpoint on the map view
    mapView.setViewpoint(new Viewpoint(64.3286, -15.5314, 72223));

    // create a graphics overlay for the graphics
    graphicsOverlay = new GraphicsOverlay();

    // add the graphics overlay to the map view
    mapView.getGraphicsOverlays().add(graphicsOverlay);

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

    // red square for points
    pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.SQUARE, 0xFFFF0000, 20);
    // thin green line for polylines
    lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF64c113, 4);
    // blue outline for polygons
    SimpleLineSymbol polygonLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF1396c1, 4);
    // cross-hatched interior for polygons
    fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.CROSS, 0x40FFA9A9, polygonLineSymbol);

    // add a listener for when sketch geometry is changed
    sketchEditor.addGeometryChangedListener(SketchGeometryChangedListener -> {
      stopButton.setDisable(false);
      // save button enable depends on if the sketch is valid. If the sketch is valid then set disable opposite of true
      saveButton.setDisable(!sketchEditor.isSketchValid());
      undoButton.setDisable(!sketchEditor.canUndo());
      redoButton.setDisable(!sketchEditor.canUndo());
    });
  }

  /**
   * Use the sketch editor to edit the geometry of the selected graphic.
   */
  @FXML
  private void handleEditButtonClicked() {
    stopButton.setDisable(false);
    saveButton.setDisable(true);

    // if the graphics overlay contains graphics, select the first graphic
    // and start the sketch editor based on that graphic's geometry
    if (!graphicsOverlay.getSelectedGraphics().isEmpty()) {
      graphic = graphicsOverlay.getSelectedGraphics().get(0);
      sketchEditor.start(graphic.getGeometry());
    }
  }

  /**
   * Stop the sketch editor, and allow a graphic to be selected from the map.
   */
  @FXML
  private void handleStopButtonClicked() {
    sketchEditor.stop();
    graphicsOverlay.clearSelection();
    disableButtons();
    // set text to inform the user the sketch is disabled
    stopButton.setDisable(false);
    // allow graphics to be selected after stop button is used.
    selectGraphic();
  }

  /**
   * Clear selection of any graphic in the graphics overlay and start a new point sketch.
   */
  @FXML
  private void handlePointButtonClicked() {
    graphicsOverlay.clearSelection();
    sketchEditor.start(SketchCreationMode.POINT);
  }

  /**
   * Clear selection of any graphic in the graphics overlay and start a new multipoint sketch.
   */
  @FXML
  private void handleMultipointButtonClicked() {
    graphicsOverlay.clearSelection();
    sketchEditor.start(SketchCreationMode.MULTIPOINT);
  }

  /**
   * Clear selection of any graphic in the graphics overlay and start a new polyline sketch.
   */
  @FXML
  private void handlePolylineButtonClicked() {
    graphicsOverlay.clearSelection();
    sketchEditor.start(SketchCreationMode.POLYLINE);
  }

  /**
   * Clear selection of any graphic in the graphics overlay and start a new polygon sketch.
   */
  @FXML
  private void handlePolygonButtonClicked() {
    graphicsOverlay.clearSelection();
    sketchEditor.start(SketchCreationMode.POLYGON);
  }

  /**
   * Clear selection of any graphic in the graphics overlay and start a  new freehand polyline sketch.
   */
  @FXML
  private void handleFreehandPolylineButtonClicked() {
    graphicsOverlay.clearSelection();
    sketchEditor.start(SketchCreationMode.FREEHAND_LINE);
  }

  /**
   * Clear selection of any graphic in the graphics overlay and start a new freehand polygon sketch.
   */
  @FXML
  private void handleFreehandPolygonButtonClicked() {
    graphicsOverlay.clearSelection();
    sketchEditor.start(SketchCreationMode.FREEHAND_POLYGON);
  }

  /**
   * Clear selection of any graphic in the graphics overlay and start a new rectangle sketch.
   */
  @FXML
  private void handleRectangleButtonClicked() {
    graphicsOverlay.clearSelection();
    sketchEditor.start(SketchCreationMode.RECTANGLE);
  }

  /**
   * Undo the last change made to the sketch, whilst sketching is active.
   */
  @FXML
  private void handleUndoButtonClicked() {
    if (sketchEditor.canUndo()) {
      sketchEditor.undo();
    }
  }

  /**
   * Redo the last change made to the sketch, whilst sketching is active.
   */
  @FXML
  private void handleRedoButtonClicked() {
    if (sketchEditor.canRedo()) {
      sketchEditor.redo();
    }
  }

  /**
   * Save the sketched graphic to the graphics overlay, and set its symbol to the type relevant for the geometry.
   */
  @FXML
  private void handleSaveButtonClicked() {

    Geometry sketchGeometry = sketchEditor.getGeometry();

    // if an existing graphic is being edited: get the selected graphic, set its geometry to that of the sketch editor geometry
    // if a new graphic: create a new graphic based on the sketch editor geometry and set symbol depending on geometry type
    if (sketchGeometry != null) {
      if (!graphicsOverlay.getSelectedGraphics().isEmpty()) {
        graphic = graphicsOverlay.getSelectedGraphics().get(0);
        graphic.setGeometry(sketchGeometry);
      } else {
        graphic = new Graphic(sketchGeometry);

        switch (sketchGeometry.getGeometryType()) {
          case POLYGON:
            graphic.setSymbol(fillSymbol);
            break;
          case POLYLINE:
            graphic.setSymbol(lineSymbol);
            break;
          case POINT:
          case MULTIPOINT:
            graphic.setSymbol(pointSymbol);
        }
        graphicsOverlay.getGraphics().add(graphic);
      }
    }
    sketchEditor.stop();

    // allow the user to select a graphic from the map view
    selectGraphic();
    graphicsOverlay.clearSelection();
    disableButtons();

    if (!graphicsOverlay.getGraphics().isEmpty()) {
      clearButton.setDisable(false);
    }
    stopButton.setDisable(true);
  }

  /**
   * Clear the graphics overlay of any saved graphics.
   */
  @FXML
  private void handleClearButtonClicked() {

    graphicsOverlay.getGraphics().clear();
    sketchEditor.stop();
    disableButtons();
  }

  /**
   * Allows the user to select a graphic from the graphics overlay.
   */
  private void selectGraphic() {

    mapView.setOnMouseClicked(e -> {
      graphicsOverlay.clearSelection();
      Point2D mapViewPoint = new Point2D(e.getX(), e.getY());

      // get graphics near the clicked location
      ListenableFuture<IdentifyGraphicsOverlayResult> identifyGraphics;
      identifyGraphics = mapView.identifyGraphicsOverlayAsync(graphicsOverlay, mapViewPoint, 10, false);

      identifyGraphics.addDoneListener(() -> {
        try {
          if (!identifyGraphics.get().getGraphics().isEmpty()) {
            // store the selected graphic
            graphic = identifyGraphics.get().getGraphics().get(0);
            graphic.setSelected(true);
            editButton.setDisable(false);
          } else {
            editButton.setDisable(true);
          }
        } catch (Exception x) {
          // on any error, display the stack trace
          x.printStackTrace();
        }
      });
    });
  }

  /**
   * Disable all UI buttons.
   */
  private void disableButtons() {

    clearButton.setDisable(true);
    redoButton.setDisable(true);
    undoButton.setDisable(true);
    editButton.setDisable(true);
    saveButton.setDisable(true);
    stopButton.setDisable(true);
  }

  /**
   * Disposes of application resources.
   */
  void terminate() {

    if (mapView != null) {
      mapView.dispose();
    }
  }
}

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