In ArcGIS Pro SDK, how do you get the position of a found layer in a map?

365
1
06-10-2020 08:20 AM
PascalVezina
Occasional Contributor II

...

Layer myLayer = null;

String sName = "ABC123";

Map map = MapView.Active.Map;

IEnumerable<Layer> matches = map.GetLayersAsFlattenedList().Where(l => l.Name.IndexOf(sName, StringComparison.CurrentCultureIgnoreCase) >= 0);

int index = 0;

if (matches.Count() == 1)
{
myLayer = matches.FirstOrDefault();

//index =  position???
map.RemoveLayer(myLayer);

}

How do you insert it at the same location I originally found it ?

myLayer = LayerFactory.Instance.CreateGroupLayer(MapView.Active.Map, index, sName);

Thanks,

Tags (1)
0 Kudos
1 Reply
serbanmarin1
New Contributor II

By 'position' I'm assuming you are referring to the position in the Layers collapsible group in the Contents pane.

You can find the index by iterating over the layer list MapView.Map.Layers via a for or foreach loop.

for(int index = 0; index < MapView.Map.Layers.Count; index++)
   if (myLayer == MapView.Map.Layers[index])
      return index;

Also when I did something similar I found a problem in SDK version 2.4 where LayerFactory.Instance.CreateGroupLayer() would only add the layer group to the 2D layers list, which may not be what you want if working with a 3D scene. You will have to add it as its own layer.

0 Kudos