Error: "layer does not exist" when having more than 1 map frame in layout

357
1
09-15-2020 12:45 PM
helenchu
Occasional Contributor II

All,

1/ I do query and zoom to selection in layout view.

2/ If I have only 1 map frame, the below codes work just fine.

3/ When I added a another map frame for locator map, I got the error "layer does not exist".  I guess I have to specify which map it should use but I don't know how.  Thanks for your help.

var myLyr =
 MapView.Active.Map.FindLayers(featureLayerName).FirstOrDefault() as FeatureLayer;
 if (myLyr == null) throw new Exception(string.Format("The feature class: {0} does not exist", featureLayerName));
0 Kudos
1 Reply
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I think the problem is that you use the Active MapView to query for the feature class.  You need to get the map frame in your layout, then use its map to find your feature layer like this - needless to say you need to know the name of the map frame you want to zoom - note you need to zoom to selection instead of the layer which is the 2nd parameter of the SetCamera call:

//Perform on the worker thread
await QueuedTask.Run(() =>
{
  //Reference MapFrame
  MapFrame mf = layout.FindElement("Map Frame") as MapFrame;

  //Reference map and layer
  Map m = mf.Map;
  FeatureLayer lyr = m.FindLayers("GreatLakes").First() as FeatureLayer;

  //Set the map frame extent to selected features in the layer (2nd parameter)
  mf.SetCamera(lyr, true);
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

in case you don't know how to get the layout use this (note that i used the Pro SDK help documentation specifically the ProSnippets for layouts to find these code snippets @ ProSnippets Layouts · Esri/arcgis-pro-sdk Wiki · GitHub 😞

// Reference a layoutitem in a project by name
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));
if (layoutItem != null)
{

  //Perform on the worker thread
  QueuedTask.Run(() =>
  {
    // Reference and load the layout associated with the layout item
    Layout layout = layoutItem.GetLayout();
    if (layout != null)
    {
....
    }
  }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos