Adding multiple layers in specific ORDER.

3809
4
Jump to solution
05-28-2015 05:01 AM
BledarBirbo1
Occasional Contributor

Hi.

I am adding 1 basemap and 6 feature layers to a map.

The problem is that the "OpenAsync" call is causing the map to add the layers with a different order that i initially called them.

Please take a look at the following code:

void _mapView_LayerLoaded(object sender, LayerLoadedEventArgs e)

{

       if (e.Layer.ID == "basemap")

       {

                loadOtherLayers();

       }

}

void loadOtherLayers()

           load_feature_layer_extras((int)LayerIdEnum_Extra.LayerAreas, "AREA");

           load_feature_layer_extras((int)LayerIdEnum_Extra.LayerBlocks, "BLOCKS");

           load_feature_layer_extras((int)LayerIdEnum_Extra.LayerRoads, "ROAD NETWORK");

           load_feature_layer_extras((int)LayerIdEnum.LayerLinks, "LINKS");

           load_feature_layer_extras((int)LayerIdEnum.LayerContainers, "CONTAINERS");

           load_feature_layer_extras((int)LayerIdEnum.LayerNodes, "NODES");

}

private async void load_feature_layer_extras(int layerId, string layerName)

{

          var featureLayer = await Esri.ArcGISRuntime.Data.ServiceFeatureTable.OpenAsync(new Uri(unetworkServiceURL_Extra + string.Format("FeatureServer/{0}", layerId)));

          featureLayer.Mode = QueryMode.OnDemand;

          var flayer = new FeatureLayer(featureLayer)

          {

              ID = layerId.ToString(),

              DisplayName = layerName,

              IsVisible = false

          };

          _mapView.Map.Layers.Add(flayer);


}

How do i make sure that all 6 layers ends up in the same order that i called them ?

Thanks in Advance.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
dotMorten_esri
Esri Notable Contributor

Change this:

private async void load_feature_layer_extras(int layerId, string layerName)

to this:

private async Task load_feature_layer_extras(int layerId, string layerName)

And then put "await" in front of all your calls to load_feature_layer_extras.

As much as possible you should/must avoid "async void". It can cause a lot of problem - especially if one layer fails to load. When you can't avoid "async void" (usually in event handlers), make sure you try/catch any awaits.

I would really encourage you to watch the following short series on use of async/await:

Six Essential Tips for Async | Channel 9

View solution in original post

4 Replies
MichaelBranscomb
Esri Frequent Contributor

Hi,

Based on your code, each layer will be added when the OpenAsync call returns. If you want to added them in a specific order you either need to await each call to load_feature_layer_extras, or consider managing the index positions at which you want to insert the layers. The default behavior of ArcGIS is usually to insert layers in this order:

- Points

- Lines

- Polygons

Therefore you might even consider implementing similar behavior, depending on the context of your app.

Cheers

Mike

0 Kudos
BledarBirbo1
Occasional Contributor

Hi Mike.

Thanks for your quick reply.

Can you please give me more specific directions, or preferably a bit of code for the option "need to await each call to load_feature_layer_extras" ?

Thanks.

0 Kudos
MiriRevivo2
New Contributor

Hi Bledar,

I think it would be helpful for you to read about asynchronous methods in general, and also specifically about the async-await pattern - Asynchronous Programming with Async and Await (C# and Visual Basic)

Your load_feature_layer_extras is an asynchronous method, and therefore all the calls to it occur one after the other, but once the execution of each begins - it's impossible to determine which will end first. So what Mike meant is that you can await your calls to the async method, and this will make it behave in a synchronous-like manner - and the order of the calls to the method will also be the order of their execution and the order in which they will end their execution.

Hope this helps,

Miri

0 Kudos
dotMorten_esri
Esri Notable Contributor

Change this:

private async void load_feature_layer_extras(int layerId, string layerName)

to this:

private async Task load_feature_layer_extras(int layerId, string layerName)

And then put "await" in front of all your calls to load_feature_layer_extras.

As much as possible you should/must avoid "async void". It can cause a lot of problem - especially if one layer fails to load. When you can't avoid "async void" (usually in event handlers), make sure you try/catch any awaits.

I would really encourage you to watch the following short series on use of async/await:

Six Essential Tips for Async | Channel 9