Toolkit 100.x - MeasureToolbar - Programmatically Clearing Feature Selection

07-24-2018 08:17 AM
New Contributor

In adapting an ArcEngine-based program to ArcGIS Runtime, the Toolkit has been handy in providing a nice implementation for the Measure tool and Compass capability that was present before.  In our case, when we're purposefully hiding the MeasureToolbar, we want to ensure that whatever the user was doing is not visible anymore.  The MeasureToolbar makes its SketchEditors for the Length and Area measurement available through LineSketchEditor and AreaSketchEditor, so it's possible to just call ClearGeometry() on those, or just use the MapView's SketchEditor's ClearGeometry() method.

On the other hand, the Feature measurement option works a bit differently.  I took a peek at the code, and it appears to create a GraphicsOverlay upon demand and add it to the MapView whenever the user first selects a feature.  The problem is that there appears to be no simple way to make changes to this GraphicsOverlay without either rudimentary determinations or more complex determinations to ensure you know which GraphicsOverlay is the correct one.  Fortunately, in our case, we don't add GraphicsOverlays to the MapView, so it's always the first one -- if any exist.

Ultimately, what I'm wondering is if it's possible to either:

  1. Retrieve the specific GraphicsOverlay that the MeasureToolbar uses.
  2. Request that the MeasureToolbar execute the same functionality as pressing the Clear button.

To note, we're using 100.2.1-beta3, but I've also tested 100.3.0-beta4 to see if it has any fixes to lingering feature selections, and I don't see anything with a cursory glance and some test runs.

0 Kudos
1 Reply
Esri Regular Contributor

Thanks for your feedback. We don't assign a name to this GraphicsOverlay but some other characteristics of it is it will always have just one selected graphic which gets added after a successful identify on tapped event.

You can use Linq query to get it once then clear Graphics or toggle visibility when MeasureToolbar is no longer active.

var overlay = mapView.GraphicsOverlays.FirstOrDefault(o => string.IsNullOrEmpty(o.Id) && o.Graphics.Count == 1 && o.Graphics[0].IsSelected);

I hope this works for you too.

Another way would be to get it once from GraphicsOverlays.CollectionChanged event after GeoViewTapped but perhaps it's better to just use code above.

private Task<GraphicsOverlay> GetMeasureResultOverlayAsync()
    var tcs = new TaskCompletionSource<GraphicsOverlay>();
    GraphicsOverlay overlay = null;
    EventHandler<GeoViewInputEventArgs> tappedHandler = null;
    NotifyCollectionChangedEventHandler collectionChangedHandler = null;
    tappedHandler = (s, e) =>
        if (collectionChangedHandler == null)
            collectionChangedHandler = (a, b) =>
                if (overlay == null && b.NewItems?.Count == 1)
                    overlay = b.NewItems[0] as GraphicsOverlay;
                         if(string.IsNullOrEmpty(overlay.Id) && overlay.Graphics.Count == 1 && overlay.Graphics[0].IsSelected)
                              mapView.GeoViewTapped -= tappedHandler;
                              mapView.GraphicsOverlays.CollectionChanged -= collectionChangedHandler;
            mapView.GraphicsOverlays.CollectionChanged += collectionChangedHandler;
    mapView.GeoViewTapped += tappedHandler;
    return tcs.Task;
0 Kudos