Layer ordering not honoured in TOC

508
4
02-14-2023 07:37 AM
Vidar
by
Occasional Contributor II

Hi,

I am trying to add 3 layers to a group layer in a specific index order. I use the FeatureLayerCreationParams object with the LayerFactory.Instance.CreateLayer() function - but when I look at the results in the TOC - the layers have come in a random order and not the order I specified.

This was working fine in 2.x but since I have moved to 3.x this code does not seem to work - I'm not sure what I can do but I need to fix the problem urgently.

Here is the code I use (I pass in my object called "layer", which holds the index information to pass on):

 

 

await QueuedTask.Run(() =>
{
	Item stagingLayer = ItemFactory.Instance.Create(AgolId, ItemFactory.ItemType.PortalItem);
	if (LayerFactory.Instance.CanCreateLayerFrom(stagingLayer))
	{
		var featureCreateParams = new FeatureLayerCreationParams(stagingLayer)
		{
			IsVisible = layer.Visible,
			MapMemberIndex = layer.LayerOrder,
			
			//Even this does NOT fix the issue
			MapMemberPosition = MapMemberPosition.Index,

			Name = layer.Name
		};

		LayerFactory.Instance.CreateLayer<FeatureLayer>(featureCreateParams, groupLayer);
	}
});

 

 

 

 

0 Kudos
4 Replies
Vidar
by
Occasional Contributor II

I'm suspecting that the constructor is at fault here - if ESRI could confirm this as a bug or not that would be good.

I have since changed the code to this, not using a constructor, and it works perfectly. Very strange and certainly a high chance that this will "can-trip" a lot of developers.

 

Here is the code that now works:

Item currentItem = ItemFactory.Instance.Create(AgolId, ItemFactory.ItemType.PortalItem);
	await QueuedTask.Run(() =>
	{
		//Create a LayerCreationParam - constructor method does not honour indexes.
		var layerParam = new LayerCreationParams(currentItem);
		layerParam.Name = layer.Name;
		layer.Visible = layer.Visible;
		layerParam.MapMemberPosition = MapMemberPosition.Index;
		layerParam.MapMemberIndex = layer.LayerOrder;

		if (LayerFactory.Instance.CanCreateLayerFrom(currentItem))
		{
			LayerFactory.Instance.CreateLayer<FeatureLayer>(layerParam, groupLayer);
		}
	});
0 Kudos
NarelleChedzey
Esri Contributor

Hi, 

 

You seem to be missing setting the MapMemberPosition to MapMemberPosition.Index in the first code snippet (when you are using the FeatureLayerCreationParams).    The default value for this property when it is not set is AutoArrange which means that the MapMemberIndex value is ignored. 

 

Thanks

Narelle

 

0 Kudos
Vidar
by
Occasional Contributor II

After I wrote the initial post, I found that missing bit of code that you mentioned (MapMemberPosition.Index) and thought I'd cracked it! and you'd think that would fix it wouldn't you?

Well, I did try that and it made zero difference. I was so frustrated and I really couldn't think what else it would be.  It's only when I stumbled across an example piece of ESRI help code showing the non-constructor example of using LayerCreationParams, that it actually started working.

I have updated the post - so people, like yourself don't think "MapMemberPosition.Index is missing". I should have done that in the first place really.

I would be interested if you can confirm my findings though if you have chance.  I am using 3.x version of Pro.

0 Kudos
NarelleChedzey
Esri Contributor

Hi, 

I'm sorry;  I'm not able to duplicate your problem with FeatureLayerCreationParams.  I have a group layer containing 3 layers (pointing to a file geodatabase).  Then I run the following code which inserts a hosted feature layer at index 2 in the group layer.   The new layer is inserted at the correct location. 

Can you run this code and see if it works as you expect?  

 

Narelle

protected override void OnClick()
{
  var groupLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<GroupLayer>().FirstOrDefault();
  if (groupLayer == null)
    return;

  // Airports_OPSNET45
  string agolId = "8c1854a8580543b0a4848a0b5fbb0791";
  int index = 2;

  DoIt(agolId, "My layer", index, groupLayer);
}

internal Task DoIt(string AgolId, string name, int index, GroupLayer groupLayer)
{
  return QueuedTask.Run(() =>
  {
    Item stagingLayer = ItemFactory.Instance.Create(AgolId, ItemFactory.ItemType.PortalItem);
    if (LayerFactory.Instance.CanCreateLayerFrom(stagingLayer))
    {
      var featureCreateParams = new FeatureLayerCreationParams(stagingLayer)
      {
        IsVisible = true, 
        MapMemberIndex = index,
        MapMemberPosition = MapMemberPosition.Index,
        Name = name
      };

      Lactory.Instance.CreateLayer<FeatureLayer>(featureCreateParams, groupLayer);
    }
  });
}

 

 

 

0 Kudos