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

SketchOnMap.cppSketchOnMap.cppSketchOnMap.hSketchEditorButton.qmlSketchOnMap.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
// [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]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "SketchOnMap.h"

#include "Map.h"
#include "MapQuickView.h"

#include "PolygonBuilder.h"
#include "SimpleFillSymbol.h"
#include "SimpleMarkerSymbol.h"
#include "SketchEditor.h"

using namespace Esri::ArcGISRuntime;

SketchOnMap::SketchOnMap(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISImagery, this))
{
  // Create a GraphicsOverlay to save the sketches to
  m_sketchOverlay = new GraphicsOverlay(this);
  createSymbols();
}

SketchOnMap::~SketchOnMap() = default;

void SketchOnMap::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<SketchOnMap>("Esri.Samples", 1, 0, "SketchOnMapSample");
}

MapQuickView* SketchOnMap::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void SketchOnMap::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);
  m_mapView->setViewpointCenter(Point(-15.5314, 64.3286, SpatialReference::wgs84()), 100'000);

  m_mapView->graphicsOverlays()->append(m_sketchOverlay);

  m_sketchEditor = new SketchEditor(this);
  m_mapView->setSketchEditor(m_sketchEditor);

  emit mapViewChanged();
}

void SketchOnMap::setSketchCreationMode(SampleSketchMode sketchCreationMode)
{
  switch(sketchCreationMode)
  {
    case SampleSketchMode::PointSketchMode:
      m_sketchEditor->start(SketchCreationMode::Point);
      break;

    case SampleSketchMode::MultipointSketchMode:
      m_sketchEditor->start(SketchCreationMode::Multipoint);
      break;

    case SampleSketchMode::PolylineSketchMode:
      m_sketchEditor->start(SketchCreationMode::Polyline);
      break;

    case SampleSketchMode::PolygonSketchMode:
      m_sketchEditor->start(SketchCreationMode::Polygon);
      break;

    default:
      break;
  }
  emit sketchEditorStartedChanged();
}

void SketchOnMap::stopSketching(bool saveGeometry)
{
  if (!m_sketchEditor->isStarted() || !saveGeometry)
  {
    m_sketchEditor->stop();
    emit sketchEditorStartedChanged();
    return;
  }

  if (!m_sketchEditor->isSketchValid())
  {
    qWarning() << "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
  Geometry sketchGeometry = m_sketchEditor->geometry();
  Symbol* geometrySymbol = nullptr;

  switch (m_sketchEditor->creationMode())
  {
    case SketchCreationMode::Point:
      geometrySymbol = m_pointSymbol;
      break;

    case SketchCreationMode::Multipoint:
      geometrySymbol = m_multiPointSymbol;
      break;

    case SketchCreationMode::Polyline:
      geometrySymbol = m_lineSymbol;
      break;

    case SketchCreationMode::Polygon:
      geometrySymbol = m_polygonSymbol;
      break;

    default:
      return;
  }

  Graphic* sketchGraphic = new Graphic(sketchGeometry, geometrySymbol, this);
  m_sketchOverlay->graphics()->append(sketchGraphic);

  m_sketchEditor->stop();
  emit sketchEditorStartedChanged();
}

bool SketchOnMap::sketchEditorStarted() const
{
  if (m_sketchEditor)
    return m_sketchEditor->isStarted();

  return false;
}

void SketchOnMap::createSymbols()
{
  m_pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Square, QColor(255, 0, 0), 10, this);
  m_multiPointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Square, QColor(0, 0, 255), 10, this);
  m_lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(144, 238, 144), 3, this);
  m_polygonSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, QColor(67, 166, 198, 119),
                                         new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor(67, 166, 198), 2.0, this), this);
}

void SketchOnMap::deleteVertex()
{
  if (m_sketchEditor->geometry().extent().isValid())
    m_sketchEditor->removeSelectedVertex();
}

void SketchOnMap::clearGraphics()
{
  m_sketchOverlay->graphics()->clear();
}

void SketchOnMap::undo()
{
  m_sketchEditor->undo();
}

void SketchOnMap::redo()
{
  m_sketchEditor->redo();
}

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