I am new to coding, and have used the above code in an ArcGIS Pro button. However, I cannot seem to get any code to work after zooming to the states layer in the private Task<Envelope> SelectStateAsync(Map m) method. For example, if I want to create a new map frame and add it to the layout after that, why won't the code below work? Anything starting at the "public void BuildEnvelopeGeometry()" won't work.
protected override async void OnClick()
{
{
Map m = MapView.Active.Map;
if (m != null)
{
var env = await SelectStateAsync(m);
if (env != null) await MapView.Active.ZoomToAsync(env);
}
}
}
private Task<Envelope> SelectStateAsync(Map m)
{
return QueuedTask.Run<Envelope>(() =>
{
Envelope selectionEnvelope = null;
FeatureLayer fl1 = m.FindLayers("ProjectArea1").FirstOrDefault() as FeatureLayer;
if (fl1 == null) return selectionEnvelope;
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().Clone();
//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").FirstOrDefault() as FeatureLayer;
if (fl2 != null)
{
// this selects the feature(s) in fl2
var selection = fl2.Select(spatialQuery);
// this get the envelope
using (RowCursor rowCursor2 = selection.Search())
{
if (rowCursor2.MoveNext())
{
// stop with the first extent
Geometry geo1 = (rowCursor2.Current as Feature).GetShape().Clone();
selectionEnvelope = geo1.Extent.Expand(1.1, 1.1, true);
}
}
}
}
return selectionEnvelope;
}
});
}
public void BuildEnvelopeGeometry()
{
//Build Envelope Geometry for Main Map Frame
Envelope mf_env_Main = EnvelopeBuilder.CreateEnvelope(new Coordinate2D(0.5, 2.5), new Coordinate2D(8.0, 10.5));
//Reference map, create MF and add to layout
MapProjectItem mapPrjItem_Main = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(item => item.Name.Equals("Map"));
Map mfMap_Main = mapPrjItem_Main.GetMap();
MapFrame mfELM_Main = LayoutElementFactory.Instance.CreateMapFrame(LayoutView.Active.Layout, mf_env_Main, mfMap_Main);
mfELM_Main.SetName("Main Map");
}
}
}