Remove and add GraphicsOverlay to MapView object.

2593
2
09-01-2017 02:51 AM
GeorgeArnaut
New Contributor II

Hi everyone my name is George. I'm using ArcGIS 100.0 for qt version 5.9. I want  to remove  graphics overlays from my map, without recreating MapView, Map and other. My application have to receive coordinates, and display them on the map. As reference I've used  Simple Renderer from arcgis examples (It shows how to add and display point on the map). But i want to update position of Objects on my map during my application execution. How can I do that? Is it possible?

Simple example of what I've used, and what i want to do.  

---------------------------------------------------------------------------------------------------------------------------------------------------------------

// Create a map using the imagery basemap
  m_map = new Map(Basemap::imagery(this), this);

  // create graphics overlay
  m_graphicsOverlay = new GraphicsOverlay();

  // create red cross SimpleMarkerSymbol
  SimpleMarkerSymbol* crossSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Cross, QColor("red"), 12, this);

  // create renderer and set symbol to crossSymbol
  SimpleRenderer* simpleRenderer = new SimpleRenderer(crossSymbol, this);
  // set the SimpleRenderer to the GraphicsOverlay
  m_graphicsOverlay->setRenderer(simpleRenderer);

  // create points to render
  Point oldFaithfulPoint(-110.828140, 44.460458, SpatialReference::wgs84());
  Point cascadeGeyserPoint(-110.829004, 44.462438, SpatialReference::wgs84());
  Point plumeGeyserPoint(-110.829381, 44.462735, SpatialReference::wgs84());

  // create graphics using points and add them to GraphicsOverlay
  addPoint(oldFaithfulPoint);
  addPoint(cascadeGeyserPoint);
  addPoint(plumeGeyserPoint);

  // Set map to map view
  m_mapView->setMap(m_map);

  // set viewpoint using the two farthest points as an envelope with padding
  m_mapView->setViewpointGeometry(Envelope(oldFaithfulPoint, plumeGeyserPoint), 50);

  // add GraphicsOverlay to MapView
  m_mapView->graphicsOverlays()->append(m_graphicsOverlay);

------------------------------------------------------------------------------------------------------------------------------------------------------------

For simplicity, I want to delete all objects that exist on a map, and reload map with new.

0 Kudos
2 Replies
LukeSmallwood
Esri Contributor

Hi George, thanks for trying out the API (I'd recommend you check out version 100.1 when you have time as well).

From the SimpleRenderer sample the addPoint code looks something like this

// create graphic

Graphic* graphic = new Graphic(point, this);

// add graphic to Graphic Overlay

m_graphicsOverlay->graphics()->append(graphic);

Here, the Graphic* object is added to a GraphicListModel* by calling the append function. I think you have 2 options to update the positions:

1 - keep track of the Graphics that you added and call setGeometry (Graphic Class | ArcGIS for Developers) with your new Point as the geometry that you pass in. One way of doing this might be to keep a separate structure which maps your points names to the Graphic - e.g. QMap<QString,Graphic*> where the key would be "oldFaithfulPoint" etc. and the value would be the Graphic* object you appended to the model.

2 - remove all of the graphics from the list model and re-add them for your new positions. You can call clear (GraphicListModel Class | ArcGIS for Developers ) on the list model to remove the graphics. Since these Graphic* objects were created with "this" as their parent QObject (presumably your app) you will also need to delete these pointers in some way to free the memory. An alternative is to give them a parent QObject* which you delete and then recreate whenever you clear the model.

I hope that helps,

Luke

0 Kudos
GeorgeArnaut
New Contributor II

Thanks Luke, it was very useful advice! I've use some of your recommendations. I have already made some steps in this direction.