Draw and clear on Map tool mouse move

1319
3
Jump to solution
06-03-2020 03:13 PM
Sai_PhaneendraPoludasu2
New Contributor III

To represent a graphic moving along with mouse cursor, I am adding a point graphic on mouse move and clear it when mouse move is triggered at a new location and a graphic for new location. But the graphics never gets cleared. They stay on map. Here is the code snippet

        private IDisposable PtGraphic;

        protected override void OnToolMouseMove(MapViewMouseEventArgs e)
        {
            QueuedTask.Run(() =>
            {
                var pt = ActiveMapView.ClientToMap(e.ClientPoint);
                ClearGraphic(ref PtGraphic);
                PtGraphic = AddGraphicToMap(pt, ColorFactory.Instance.RedRGB, 3, SimpleMarkerStyle.Circle);

            });

            base.OnToolMouseMove(e);
        }

        private void ClearGraphic(ref IDisposable graphic)
        {
            if (graphic != null)
                graphic.Dispose();
            graphic = null;
        }

        public static async Task<IDisposable> AddGraphicToMap(Geometry geom, CIMColor color,
            double size = 1.0, SimpleMarkerStyle markerStyle = SimpleMarkerStyle.Rectangle)
        {
            ///Build Symbology

            return MapView.Active.AddOverlay(geom, symbol);
        }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Am I missing something ?

Appreciate any help.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JohnJones
Esri Contributor

I believe there is an error in this code as AddGraphicToMap returns a Task<IDisposable> which is apparently being converted to an IDisposable (PtGraphic) when what you want to do is await the Task to extract the IDisposable that results, you can do this by marking the lambda you pass to QueuedTask.Run as 'async' and then add 'await' on the call to AddGraphicToMap.  There were some timing related bugs in this area installed during 2.6 development. 

Also I believe you should consider using DelayedInvoker https://pro.arcgis.com/en/pro-app/sdk/api-reference/#topic10949.html so you don't handle every mouse move event but throttle to handle mouse move events as fast as they can be processed.

View solution in original post

0 Kudos
3 Replies
JohnJones
Esri Contributor

I believe there is an error in this code as AddGraphicToMap returns a Task<IDisposable> which is apparently being converted to an IDisposable (PtGraphic) when what you want to do is await the Task to extract the IDisposable that results, you can do this by marking the lambda you pass to QueuedTask.Run as 'async' and then add 'await' on the call to AddGraphicToMap.  There were some timing related bugs in this area installed during 2.6 development. 

Also I believe you should consider using DelayedInvoker https://pro.arcgis.com/en/pro-app/sdk/api-reference/#topic10949.html so you don't handle every mouse move event but throttle to handle mouse move events as fast as they can be processed.

0 Kudos
Sai_PhaneendraPoludasu2
New Contributor III

Thanks John Jones‌. After looking at your answer, I changed the Add Graphic method to a non-async method and just returned IDisposable instead of Task<IDisposable>. It's working good for now, but I observe a slight delay, I will take a look at the DelayedInvoker. Cheers!

0 Kudos
DaveLewis73
Occasional Contributor

John,

I realize this is an old posting.  However, I was hoping you could tell me how to implement DelayedInvoker.  I can't find any examples or documentation on it.

Thank you.

0 Kudos