AnsweredAssumed Answered

How to convert selected feature from feature class to geometry

Question asked by joneil1414 on Nov 14, 2018
Latest reply on Dec 6, 2018 by gkmieliauskas_cellexp

Hello,

I cannot figure out how to convert selected features from a feature dataset to geometry, so that I can zoom the map to the selected features (I want to zoom the map to only the selected state from the code below).

 

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

Outcomes