How to zoom to selected feature using ArcGIS Pro SDK

2404
1
11-02-2018 01:54 PM
JoshuaO_Neil
New Contributor II

Hello,

I have two layers in my map (project area and states). I was able to select the state from the states layer that intersects my project area polygon using the code below. However, I cannot figure out how to zoom the map to the selected state (the state that intersects with my project area boundary). I thought the last 2 lines of my code would do it, but that zooms the map to the entire states layer (not just the selected state). Does anyone know what I am doing wrong?

{
var layout = Project.Current.GetItems<LayoutProjectItem>()?.First().GetLayout();
if (layout == null) return;
MapFrame mf = layout.FindElement("Main Map") as MapFrame;
Map m = mf.Map;


FeatureLayer fl1 = m.FindLayers("ProjectArea1").First() as FeatureLayer;
QueryFilter queryFilter = new QueryFilter();


using (ArcGIS.Core.Data.RowCursor rowCursor = fl1.Search(queryFilter = null))
{
//Grab the first record (and hopefully only record)
while (rowCursor.MoveNext())
{
//Grab the features geometry
Feature feature1 = rowCursor.Current as Feature;
Geometry geo = feature1.GetShape();

//Set up a spatial query
var spatialQuery = new SpatialQueryFilter()
{ FilterGeometry = geo, SpatialRelationship = SpatialRelationship.Intersects };

//Reference county layer and apply spatial query
FeatureLayer fl2 = m.FindLayers("States").First() as FeatureLayer;
fl2.Select(spatialQuery);

Geometry geo1 = fl2.QueryExtent();

Envelope env = geo1.Extent;
mf.ZoomTo(env);
}
}

Tags (1)
0 Kudos
1 Reply
stevegourley
Occasional Contributor II

I don't understand what Select does but it doesn't seem to do what we both assumed it did. I ended up using the map's set selection method and then zoomed to the selection.

MapView.Active.Map.SetSelection(new Dictionary<MapMember, List<long>> {
{ layer, primaryKeys.Select(x => x.Value).ToList() }
});
MapView.Active.ZoomToSelectedAsync()
```
0 Kudos