View on GitHub Sample 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.

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::start 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

SketchEditorButton.qml SketchEditorButton.qml SketchOnMap.cpp SketchOnMap.h SketchOnMap.qml main.cpp main.qml
// [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
import QtQuick.Controls
import QtQuick.Layouts
// This component defines each of the buttons in the sketch editor control UI
RoundButton {
id: sketchEditorButton
property var columnSpan: 1
property string buttonName: ""
property string iconPath: ""
property var images: 1
Layout.fillWidth: true
Layout.columnSpan: columnSpan
radius: 5
// Set the focus policy so that the buttons do not take focus from the MapView
focusPolicy: Qt.NoFocus
Image {
id: imgComponent
anchors {
horizontalCenter: parent.horizontalCenter
verticalCenter: parent.verticalCenter
verticalCenterOffset: -textComponent.height/2
}
source: iconPath
width: 20
fillMode: Image.PreserveAspectFit
visible: images === 1
}
Image {
id: imgComponentLeft
anchors {
right: parent.horizontalCenter
rightMargin: -imgComponentRight.width/5
verticalCenter: parent.verticalCenter
verticalCenterOffset: -textComponent.height/2 - 2
}
source: iconPath
width: 20
fillMode: Image.PreserveAspectFit
visible: images === 2
}
Image {
id: imgComponentRight
anchors {
left: parent.horizontalCenter
leftMargin: -imgComponentLeft.width/5
verticalCenter: parent.verticalCenter
verticalCenterOffset: -textComponent.height/2
}
source: iconPath
width: 20
fillMode: Image.PreserveAspectFit
visible: images === 2
}
Text {
id: textComponent
anchors {
top: images === 1 ? imgComponent.bottom : imgComponentRight.bottom
horizontalCenter: parent.horizontalCenter
}
text: buttonName
font.pixelSize: 8
}
}