Select to view content in your preferred language

Draw on Map

1375
3
Jump to solution
08-25-2020 07:38 AM
MauricioRamirez1
Emerging Contributor

We've developed an application using the Runtime SDK with QT.  We have a new requirement to allow users to draw areas on the map.  We've seen a few examples using C# and JavaScript, unfortunately we've not been able to find an example using C++ and QT.  They need the ability to  draw freehand and also by clicking points and have it auto close the shape. From other examples I've seen we need to have a FeatureService that will allow editing, but getting the map to accept clicks as points for a polygon has me puzzled.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
TomNorby
Occasional Contributor

Hi there, here's a quick untested code snippet that hopefully nudges you in the right direction, there may be a better way to do it, but it's repurposed from a gps breadcrumb function I had and looking at the SDK sample for Geometries>Nearest Vertex:

MapGraphicsView *map_control;

std::shared_ptr<GraphicsOverlay> layer_geometries;

std::shared_ptr<PolylineBuilder> polyline_tracks;

std::shared_ptr<SimpleLineSymbol> polyline_pen;

std::shared_ptr<Graphic> polyline_graphic;

// choose your line style

polyline_pen = std::make_shared<SimpleLineSymbol>(SimpleLineSymbolStyle::Dot, QColor(0, 0, 255, 100), 5);

map_control = new MapGraphicsView(this);

layer_geometries = std::make_shared<GraphicsOverlay>();

map_control->graphicsOverlays()->append(p->layer_geometries.get());

connect(m_mapView, &MapGraphicsView::mouseClicked, this, [=](QMouseEvent& e)

{

Point clicked_location = map_control->screenToLocation(e.x(), e.y());
polyline_tracks->addPoint(clicked_location );

std::shared_ptr<PolylineBuilder>close_points(p->gps_tracks);

close_points->addPoint(p->gps_tracks->parts()->part(0)->startPoint());

polyline_graphic= std::make_shared<Graphic>(close_points->toGeometry(), polyline_pen.get());

layer_geometries->graphics()->clear();

layer_geometries->graphics()->append(p->polyline_graphic.get());

});

Edit: Check out this sample program with markup - Markup Documentation. Source code here: dynamic-situational-awareness-qt/Shared/markup

View solution in original post

0 Kudos
3 Replies
TomNorby
Occasional Contributor

Hi there, here's a quick untested code snippet that hopefully nudges you in the right direction, there may be a better way to do it, but it's repurposed from a gps breadcrumb function I had and looking at the SDK sample for Geometries>Nearest Vertex:

MapGraphicsView *map_control;

std::shared_ptr<GraphicsOverlay> layer_geometries;

std::shared_ptr<PolylineBuilder> polyline_tracks;

std::shared_ptr<SimpleLineSymbol> polyline_pen;

std::shared_ptr<Graphic> polyline_graphic;

// choose your line style

polyline_pen = std::make_shared<SimpleLineSymbol>(SimpleLineSymbolStyle::Dot, QColor(0, 0, 255, 100), 5);

map_control = new MapGraphicsView(this);

layer_geometries = std::make_shared<GraphicsOverlay>();

map_control->graphicsOverlays()->append(p->layer_geometries.get());

connect(m_mapView, &MapGraphicsView::mouseClicked, this, [=](QMouseEvent& e)

{

Point clicked_location = map_control->screenToLocation(e.x(), e.y());
polyline_tracks->addPoint(clicked_location );

std::shared_ptr<PolylineBuilder>close_points(p->gps_tracks);

close_points->addPoint(p->gps_tracks->parts()->part(0)->startPoint());

polyline_graphic= std::make_shared<Graphic>(close_points->toGeometry(), polyline_pen.get());

layer_geometries->graphics()->clear();

layer_geometries->graphics()->append(p->polyline_graphic.get());

});

Edit: Check out this sample program with markup - Markup Documentation. Source code here: dynamic-situational-awareness-qt/Shared/markup

0 Kudos
MauricioRamirez1
Emerging Contributor

Thanks. I’ll give it a try.

Sent from my iPad

0 Kudos
LucasDanzinger
Esri Frequent Contributor

@MauricioRamirez1 the 100.12 release of ArcGIS Runtime now supports a SketchEditor, which greatly simplifies the workflow for sketching geometries on a map. This might be something worth considering integrating into your app. It doesn't support freehand yet, but that is in the plans for the future. More details are in the release blog - https://community.esri.com/t5/arcgis-runtime-sdks-blog/sketcheditor-now-available-in-the-arcgis-runt... 

0 Kudos