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);
}
}
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.