Sketch on map

View inC++QMLView on GitHubSample viewer app

This sample demonstrates how to use the Sketch Editor to edit or sketch a new point, line, or polygon geometry on to a map.

screenshot

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, and polygons.

Add points or vertices by tapping on the map. You can edit individual points by tapping to select the point, then dragging it to a new location or double tapping to delete it.

Use the control panel to undo or redo changes made to the sketch, save the sketch to the graphics overlay, discard the current sketch, or clear the graphics overlay.

How it works

  1. Create a SketchEditor component and set it on the map view with MapView.setSketchEditor.
  2. Call SketchEditor.startWithCreationMode and pass in a SketchCreationMode enum to begin sketching.
  3. While sketching is in progress, call SketchEditor.undo() or SketchEditor.redo() to undo or redo edits respectively.
  4. To save the sketch, first check if the sketch is valid with SketchEditor.isSketchValid(). If it is, create a graphic using SketchEditor.geometry and add it to the map view's GraphicsOverlay.
  5. Call SketchEditor.stop() to stop sketching.

Relevant API

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

Tags

draw, edit

Sample Code

SketchOnMap.qmlSketchOnMap.qmlSketchEditorButton.qml
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// [WriteFile Name=SketchOnMap, Category=DisplayInformation]
// [Legal]
// Copyright 2021 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.
// [Legal]

import QtQuick 2.6
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.15
import Esri.ArcGISRuntime 100.15

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    enum DrawingModes {
        NotDrawing,
        Point,
        Multipoint,
        Line,
        Polygon
    }

    property int drawStatus: SketchOnMap.DrawingModes.NotDrawing

    MapView {
        id: mapView
        anchors.fill: parent

        // Force focus to remain on MapView so SketchEditor will respond to keystrokes
        onFocusChanged: focus = true;

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        Map {
            id: map
            Basemap {
                initStyle: Enums.BasemapStyleArcGISImagery
            }

            initialViewpoint: ViewpointCenter {
                Point {
                    y: 64.3286
                    x: -15.5314
                    spatialReference: SpatialReference { wkid: 4326 }
                }
                targetScale: 100000
            }
        }

        SketchEditor {
            id: sketchEditor
        }

        GraphicsOverlay {
            id: sketchOverlay
        }
    }

    // Display an option to delete the selected vertex if the user right-clicks or taps and holds when the sketch editor is started
    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.RightButton
        enabled: sketchEditor.started
        onClicked: {
            contextMenu.popup()
        }

        Menu {
            id: contextMenu
            width: actionComponent.width
            Action {
                id: actionComponent
                text: "Delete"
                onTriggered: {
                    if (!sketchEditor.geometry.extent.empty)
                        sketchEditor.removeSelectedVertex();
                }
            }
        }
    }

    SimpleMarkerSymbol {
        id: pointSymbol
        style: Enums.SimpleMarkerSymbolStyleSquare
        color: "red"
        size: 10
    }

    SimpleMarkerSymbol {
        id: multiPointSymbol
        style: Enums.SimpleMarkerSymbolStyleSquare
        color: "blue"
        size: 10
    }

    SimpleLineSymbol {
        id: polylineSymbol
        style: Enums.SimpleLineSymbolStyleSolid
        color: "#90EE90"
        width: 3
    }

    SimpleFillSymbol {
        id: polygonSymbol
        style: Enums.SimpleFillSymbolStyleSolid
        color: "#7743A6C6"
        outline: SimpleLineSymbol {
            style: "SimpleLineSymbolStyleSolid"
            width: 3
            color: "#43A6C6"
        }
    }

    Control {
        id: control
        anchors.right: parent.right
        padding: 5
        width: 110

        background: Rectangle {
            color: "black"
            opacity: .5
        }

        contentItem: ColumnLayout {
            id: columns
            anchors {
                verticalCenter: parent.verticalCenter
                horizontalCenter: parent.horizontalCenter
            }
            spacing: 20

            GridLayout {
                id: geometryColumn
                Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                columns: 2

                Text {
                    id: geometryHeader
                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                    Layout.columnSpan: 2
                    text: "Geometry"
                    color: "white"
                    font.pixelSize: 16
                    font.bold: true
                }

                SketchEditorButton {
                    id: ptButton
                    buttonName: "Point"
                    iconPath: "qrc:/Samples/DisplayInformation/SketchOnMap/iconAssets/point-32.png"
                    highlighted: drawStatus === SketchOnMap.DrawingModes.Point

                    enabled: !sketchEditor.started

                    onClicked: {
                        sketchEditor.startWithCreationMode(Enums.SketchCreationModePoint);
                        drawStatus = SketchOnMap.DrawingModes.Point;
                    }
                }

                SketchEditorButton {
                    id: mPtButton
                    buttonName: "Multipoint"
                    iconPath: "qrc:/Samples/DisplayInformation/SketchOnMap/iconAssets/point-32.png"
                    images: 2
                    highlighted: drawStatus === SketchOnMap.DrawingModes.Multipoint

                    enabled: !sketchEditor.started

                    onClicked: {
                        sketchEditor.startWithCreationMode(Enums.SketchCreationModeMultipoint);
                        drawStatus = SketchOnMap.DrawingModes.Multipoint;
                    }
                }

                SketchEditorButton {
                    id: lineButton
                    buttonName: "Line"
                    iconPath: "qrc:/Samples/DisplayInformation/SketchOnMap/iconAssets/line-32.png"
                    highlighted: drawStatus === SketchOnMap.DrawingModes.Line

                    enabled: !sketchEditor.started

                    onClicked: {
                        sketchEditor.startWithCreationMode(Enums.SketchCreationModePolyline);
                        drawStatus = SketchOnMap.DrawingModes.Line;
                    }
                }

                SketchEditorButton {
                    id: polygonButton
                    buttonName: "Polygon"
                    iconPath: "qrc:/Samples/DisplayInformation/SketchOnMap/iconAssets/polygon-32.png"
                    highlighted: drawStatus === SketchOnMap.DrawingModes.Polygon

                    enabled: !sketchEditor.started

                    onClicked: {
                        sketchEditor.startWithCreationMode(Enums.SketchCreationModePolygon);
                        drawStatus = SketchOnMap.DrawingModes.Polygon;
                    }
                }
            }

            GridLayout {
                id: editingColumn
                Layout.fillWidth: true
                Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                columns: 2

                Text {
                    id: editingHeader
                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                    Layout.columnSpan: 2
                    text: "Editing"
                    color: "white"
                    font.pixelSize: 16
                    font.bold: true
                }

                SketchEditorButton {
                    id: undoButton
                    buttonName: "Undo"
                    iconPath: "qrc:/Samples/DisplayInformation/SketchOnMap/iconAssets/undo-32.png"

                    enabled: sketchEditor.started

                    onClicked: sketchEditor.undo();
                }

                SketchEditorButton {
                    id: redoButton
                    buttonName: "Redo"
                    iconPath: "qrc:/Samples/DisplayInformation/SketchOnMap/iconAssets/redo-32.png"

                    enabled: sketchEditor.started

                    onClicked: sketchEditor.redo();
                }

                SketchEditorButton {
                    id: deleteVertexButton
                    buttonName: "Delete selected vertex"
                    iconPath: "qrc:/Samples/DisplayInformation/SketchOnMap/iconAssets/erase-32.png"
                    columnSpan: 2

                    enabled: sketchEditor.started

                    onClicked: {
                        if (!sketchEditor.geometry.extent.empty) {
                            sketchEditor.removeSelectedVertex();
                        }
                    }
                }

                SketchEditorButton {
                    id: saveEditsButton
                    buttonName: "Save edits"
                    iconPath: "qrc:/Samples/DisplayInformation/SketchOnMap/iconAssets/check-circle-32.png"
                    columnSpan: 2

                    enabled: sketchEditor.started

                    onClicked: {
                        drawStatus = SketchOnMap.DrawingModes.NotDrawing;
                        if (!sketchEditor.isSketchValid()) {
                            console.log("Unable to save sketch. Sketch is not valid.");
                            return;
                        }

                        // To save the sketch, create a graphic with the sketch's geometry before stopping the sketchEditor
                        const graphic = ArcGISRuntimeEnvironment.createObject("Graphic");
                        graphic.geometry = sketchEditor.geometry;

                        switch (sketchEditor.creationMode) {
                        case Enums.SketchCreationModePoint:
                            graphic.symbol = pointSymbol
                            break;
                        case Enums.SketchCreationModeMultipoint:
                            graphic.symbol = multiPointSymbol
                            break;
                        case Enums.SketchCreationModePolyline:
                            graphic.symbol = polylineSymbol
                            break;
                        case Enums.SketchCreationModePolygon:
                            graphic.symbol = polygonSymbol
                            break;

                        default:
                            break;
                        }

                        sketchOverlay.graphics.append(graphic);
                        sketchEditor.stop();
                    }
                }

                SketchEditorButton {
                    id: discardEditsButton
                    buttonName: "Discard edits"
                    iconPath: "qrc:/Samples/DisplayInformation/SketchOnMap/iconAssets/circle-disallowed-32.png"
                    columnSpan: 2

                    enabled: sketchEditor.started

                    onClicked: {
                        drawStatus = SketchOnMap.DrawingModes.NotDrawing;
                        sketchEditor.stop();
                    }
                }

                SketchEditorButton {
                    id: clearGraphicsButton
                    buttonName: "Clear graphics"
                    iconPath: "qrc:/Samples/DisplayInformation/SketchOnMap/iconAssets/trash-32.png"
                    columnSpan: 2
                    enabled: sketchOverlay.graphics.count > 0;

                    onClicked: {
                        sketchOverlay.graphics.clear();
                    }
                }
            }
        }
    }
}

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