Clear All Overlays on a Map

1048
3
Jump to solution
02-18-2020 02:13 PM
MKa
by
Occasional Contributor III

I have a tool that adds overlays to the map.  I then dispose of the map items when my Tool Deactivates.  But for some reason, sometimes a graphic gets left behind and there is no way to get rid of it.  Is there a programmatic way to get all the disposible items on a map and clear them or otherwise interact with them?

0 Kudos
1 Solution

Accepted Solutions
JohnJones
Esri Contributor

Thanks for the info.  I'll try to generate a repro case based on this info.  I had been assuming that your tool had awaited the completion of the Task<IDisposable>'s returned by AddOverlay and thus they had completed and the IDisposable's representing the overlay were recorded in the tool prior to deactivating the tool.  

We can confirm this is the problem if you find that your abandoned overlay's go away if you reactivate and deactivate your tool as they will by then be waiting in the Tool to be cleaned up on the now second deactivation.

I can make some changes to the MapTool to account for this possibility, and ensure that any overlays that don't get added until after your tool is deactivated are immediately removed.

View solution in original post

3 Replies
JohnJones
Esri Contributor

The various AddOverlay & UpdateOverlay methods on MapTool keep a handle to all the graphics that that MapTool has placed on the MapVIew and automatically removes them from the view when the tool is deactivated.

If you instead use the corresponding (extension) methods on the MapView itself  then the MapTool cannot track them and garbage collect for you so you are responsible for holding onto them and disposing of them at the appropriate time.

If you are experiencing a bug with the MapTool version of these commands please let us know so we can investigate and fix.  If you are experiencing problems with the MapView version you can post some of your code to help us debug what may be going wrong in your logic.

There is no way to currently find all overlays on the MapView (code that adds them is responsible for removing them appropriately) other than that closing and reopening a new MapView is the only thing I can suggest to approximate this behavior.

Thanks

0 Kudos
MKa
by
Occasional Contributor III

It seems like this is happening because my map is creating an overlay and it takes a small amount of time.  If I close the toolbox before the overlay process is done, my overlay is not proper cleaned.  I need a way to disable my close or save buttons on my tool dock if the tool is still drawing.

0 Kudos
JohnJones
Esri Contributor

Thanks for the info.  I'll try to generate a repro case based on this info.  I had been assuming that your tool had awaited the completion of the Task<IDisposable>'s returned by AddOverlay and thus they had completed and the IDisposable's representing the overlay were recorded in the tool prior to deactivating the tool.  

We can confirm this is the problem if you find that your abandoned overlay's go away if you reactivate and deactivate your tool as they will by then be waiting in the Tool to be cleaned up on the now second deactivation.

I can make some changes to the MapTool to account for this possibility, and ensure that any overlays that don't get added until after your tool is deactivated are immediately removed.