Manage operational layers

View inMAUIUWPWPFWinUIView on GitHub

Add, remove, and reorder operational layers in a map.

Image of manage operational layers

Use case

Operational layers display the primary content of the map and usually provide dynamic content for the user to interact with (as opposed to basemap layers that provide context).

The order of operational layers in a map determines the visual hierarchy of layers in the view. You can bring attention to a specific layer by rendering above other layers.

How to use the sample

When the app starts, a list displays the operational layers that are currently displayed in the map. Right-tap on the list item to remove the layer, or left-tap to move it to the top. The map will be updated automatically.

The second list shows layers that have been removed from the map. Tap one to add it to the map.

How it works

  1. Get the operational layers from the map using map.OperationalLayers.
  2. Add or remove layers using layerList.Add(layer) and layerList.Remove(layer) respectively. The last layer in the list will be rendered on top.

Relevant API

  • ArcGISMapImageLayer
  • Map
  • MapView
  • MapView.OperationalLayers

Additional information

You cannot add the same layer to the map multiple times or add the same layer to multiple maps. Instead, create a new layer using the FeatureTable.

Tags

add, delete, layer, map, remove

Sample Code

ManageOperationalLayers.xaml.csManageOperationalLayers.xaml.csManageOperationalLayers.xaml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2022 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Mapping;

namespace ArcGIS.Samples.ManageOperationalLayers
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Manage operational layers",
        category: "Map",
        description: "Add, remove, and reorder operational layers in a map.",
        instructions: "When the app starts, a list displays the operational layers that are currently displayed in the map. Right-tap on the list item to remove the layer, or left-tap to move it to the top. The map will be updated automatically.",
        tags: new[] { "add", "delete", "layer", "map", "remove" })]
    public partial class ManageOperationalLayers : ContentPage
    {
        private MapViewModel _viewModel;

        // Some URLs of layers to add to the map.
        private readonly string[] _layerUrls = new[]
        {
            "https://sampleserver5.arcgisonline.com/arcgis/rest/services/Elevation/WorldElevations/MapServer",
            "https://sampleserver5.arcgisonline.com/arcgis/rest/services/Census/MapServer",
            "https://sampleserver5.arcgisonline.com/arcgis/rest/services/DamageAssessment/MapServer"
        };

        public ManageOperationalLayers()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            // Set up the view model and bindings.
            _viewModel = new MapViewModel(new Map(BasemapStyle.ArcGISStreets));
            MyMapView.Map = _viewModel.Map;
            IncludedListView.ItemsSource = _viewModel.IncludedLayers;
            ExcludedListView.ItemsSource = _viewModel.ExcludedLayers;

            // Add the layers.
            foreach (string layerUrl in _layerUrls)
            {
                _viewModel.AddLayerFromUrl(layerUrl);
            }
        }

        private void MoveButton_OnClicked(object sender, EventArgs e)
        {
            try
            {
                // Get the clicked button.
                Button clickedButton = (Button)sender;

                // Get the clicked layer.
                Layer clickedLayer = (Layer)clickedButton.BindingContext;

                // Move the layer.
                _viewModel.MoveLayer(clickedLayer);
            }
            // Sometimes if the user clicks too quickly NREs will occur.
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private void PromoteButton_Clicked(object sender, EventArgs e)
        {
            try
            {
                // Get the clicked button.
                Button clickedButton = (Button)sender;

                // Get the clicked layer.
                Layer clickedLayer = (Layer)clickedButton.BindingContext;

                // Move the layer.
                _viewModel.PromoteLayer(clickedLayer);
            }
            // Sometimes if the user clicks too quickly NREs will occur.
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private void DemoteButton_Clicked(object sender, EventArgs e)
        {
            try
            {
                // Get the clicked button.
                Button clickedButton = (Button)sender;

                // Get the clicked layer.
                Layer clickedLayer = (Layer)clickedButton.BindingContext;

                // Move the layer.
                _viewModel.DemoteLayer(clickedLayer);
            }
            // Sometimes if the user clicks too quickly NREs will occur.
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
    }

    internal class MapViewModel
    {
        public Map Map { get; }
        public LayerCollection IncludedLayers => Map.OperationalLayers;
        public LayerCollection ExcludedLayers { get; } = new LayerCollection();

        public MapViewModel(Map map)
        {
            Map = map;
        }

        public void AddLayerFromUrl(string layerUrl)
        {
            ArcGISMapImageLayer layer = new ArcGISMapImageLayer(new Uri(layerUrl));
            Map.OperationalLayers.Add(layer);
        }

        public void PromoteLayer(Layer selectedLayer)
        {
            // Find the collection the layer is in.
            LayerCollection owningCollection;
            if (IncludedLayers.Contains(selectedLayer))
            {
                owningCollection = IncludedLayers;
            }
            else
            {
                owningCollection = ExcludedLayers;
            }

            // Get the current index (position) of the layer.
            int layerIndex = owningCollection.IndexOf(selectedLayer);

            // Skip if the layer can't be moved down because it is already at the bottom.
            if (layerIndex == owningCollection.Count - 1)
            {
                return;
            }

            // Move the layer by removing it and re-adding it at its old position plus 1.
            owningCollection.Remove(selectedLayer);
            owningCollection.Insert(layerIndex + 1, selectedLayer);
        }

        public void DemoteLayer(Layer selectedLayer)
        {
            // Find the collection the layer is in.
            LayerCollection owningCollection = IncludedLayers.Contains(selectedLayer) ? IncludedLayers : ExcludedLayers;

            // Get the current index (position) of the layer.
            int layerIndex = owningCollection.IndexOf(selectedLayer);

            // Skip if the layer can't be moved because it is already at the top.
            if (layerIndex < 1)
            {
                return;
            }

            // Move the layer by removing it and re-adding it at its old position minus 1.
            owningCollection.Remove(selectedLayer);
            owningCollection.Insert(layerIndex - 1, selectedLayer);
        }

        public void MoveLayer(Layer selectedLayer)
        {
            // Remove the layer from the list it is currently in and add it to the other list.
            if (IncludedLayers.Contains(selectedLayer))
            {
                IncludedLayers.Remove(selectedLayer);
                ExcludedLayers.Add(selectedLayer);
            }
            else
            {
                ExcludedLayers.Remove(selectedLayer);
                IncludedLayers.Add(selectedLayer);
            }
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.