Remove graphics layer doesn't work

800
2
Jump to solution
10-14-2021 03:12 PM
LeLuong
New Contributor III

All,

I'm trying to remove the graphics layer on map.  Codes did go through

 graphicsLayer.RemoveElements(graphicsLayer.GetSelectedElements());

but nothing happened.  My graphics layer is still there.  Am I missing something?  

 

 protected async override void OnClick()
    {
      //Get the graphics layer
      var graphicsLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<GraphicsLayer>().FirstOrDefault();
        await QueuedTask.Run(() =>
      {
          //Remove existing graphicsLayer
          if (graphicsLayer != null)
          {
              graphicsLayer.RemoveElements(graphicsLayer.GetSelectedElements());
          }
});

 

Thanks! 

0 Kudos
2 Solutions

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

Your code removes selected elements of graphics layer. To remove graphics layer you need to remove map layer using QueuedTask.Run like this :

MapView.Active.Map.RemoveLayer(graphicsLayer);

 

View solution in original post

LeLuong
New Contributor III

Thank you very much @GKmieliauskas .  I actually wanted to remove all elements.  I should use

graphicsLayer.RemoveElements(graphicsLayer.GetElements()); instead of 

graphicsLayer.RemoveElements(graphicsLayer.GetSelectedElements());

View solution in original post

0 Kudos
2 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Your code removes selected elements of graphics layer. To remove graphics layer you need to remove map layer using QueuedTask.Run like this :

MapView.Active.Map.RemoveLayer(graphicsLayer);

 

LeLuong
New Contributor III

Thank you very much @GKmieliauskas .  I actually wanted to remove all elements.  I should use

graphicsLayer.RemoveElements(graphicsLayer.GetElements()); instead of 

graphicsLayer.RemoveElements(graphicsLayer.GetSelectedElements());
0 Kudos