Display layer view state

View inMAUIUWPWPFWinUIView on GitHub

Determine if a layer is currently being viewed.

Image of display layer view state

Use case

The view status includes information on the loading state of layers and whether layers are visible at a given scale. You might change how a layer is displayed in a layer list to communicate whether it is being viewed in the map. For example, you could show a loading spinner next to its name when the view status is Loading, grey out the name when NotVisible or OutOfScale, show the name normally when Active, or with a warning or error icon when the status is Warning or Error.

How to use the sample

Tap the Load layer button to add a feature layer to the map. The current view status of the layer will display on the map. Zoom in and out of the map and note the layer disappears when the map is scaled outside of its min and max scale range. Control the layer's visibility with the switch. If you disconnect your device from the network and pan around the map, a warning will display. Reconnect to the network to remove the warning. The layer's current view status will update accordingly as you carry out these actions.

How it works

  1. Create a Map with an operational layer.
  2. Set the map on a MapView.
  3. Listen to LayerViewStateChanged from the map view.
  4. Get the Layer for the event with event.Layer and the current view status with event.LayerViewState.Status.

Relevant API

  • LayerViewStateChanged
  • LayerViewStateChangedEventArgs
  • Map
  • MapView

About the data

The Satellite (MODIS) Thermal Hotspots and Fire Activity layer presents detectable thermal activity from MODIS satellites for the last 48 hours. MODIS Global Fires is a product of NASA’s Earth Observing System Data and Information System (EOSDIS), part of NASA's Earth Science Data. EOSDIS integrates remote sensing and GIS technologies to deliver global MODIS hotspot/fire locations to natural resource managers and other stakeholders around the World.

Additional information

The following are members of the LayerViewStatus enum:

  • Active: The layer in the view is active.
  • NotVisible: The layer in the view is not visible.
  • OutOfScale: The layer in the view is out of scale. A status of OutOfScale indicates that the view is zoomed outside of the scale range of the layer. If the view is zoomed too far in (e.g. to a street level), it is beyond the max scale defined for the layer. If the view has zoomed too far out (e.g. to global scale), it is beyond the min scale defined for the layer.
  • Loading: The layer in the view is loading. Once loading has completed, the layer will be available for display in the view. If there was a problem loading the layer, the status will be set to ERROR.
  • Error: The layer in the view has an unrecoverable error. When the status is Error, the layer cannot be rendered in the view. For example, it may have failed to load, be an unsupported layer type, or contain invalid data.
  • Warning: The layer in the view has a non-breaking problem with its display, such as incomplete information (eg. by requesting more features than the max feature count of a service) or a network request failure.

Tags

layer, load, map, status, view, visibility

Sample Code

DisplayLayerViewState.xaml.csDisplayLayerViewState.xaml.csDisplayLayerViewState.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
// 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.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;

namespace ArcGIS.Samples.DisplayLayerViewState
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Display layer view state",
        category: "MapView",
        description: "Determine if a layer is currently being viewed.",
        instructions: "Tap the *Load layer* button to add a feature layer to the map. The current view status of the layer will display on the map. Zoom in and out of the map and note the layer disappears when the map is scaled outside of its min and max scale range. Control the layer's visibility with the switch. If you disconnect your device from the network and pan around the map, a warning will display. Reconnect to the network to remove the warning. The layer's current view status will update accordingly as you carry out these actions.",
        tags: new[] { "layer", "load", "map", "status", "view", "visibility" })]
    public partial class DisplayLayerViewState : ContentPage
    {
        // Reference to list of view status for each layer
        public DisplayLayerViewState()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            MyMapView.Map = new Map(BasemapStyle.ArcGISTopographic) { InitialViewpoint = new Viewpoint(new MapPoint(-11e6, 45e5, SpatialReferences.WebMercator), 40000000) };

            // Event for layer view state changed.
            MyMapView.LayerViewStateChanged += OnLayerViewStateChanged;
        }

        private void OnLayerViewStateChanged(object sender, LayerViewStateChangedEventArgs e)
        {
            // Check that the layer that changed is the feature layer added to the map.
            if (e.Layer == MyMapView.Map?.OperationalLayers.FirstOrDefault())
            {
                // Update the UI with the layer view status.
                LayerStatusLabel.Text = e.LayerViewState.Status.ToString();
            }
        }

        private void LoadButton_Click(object sender, EventArgs e)
        {
            LoadButton.Text = "Reload layer";
            _ = LoadLayer();
        }

        private async Task LoadLayer()
        {
            MyMapView.Map.OperationalLayers.Clear();
            LayerStatusLabel.Text = string.Empty;

            try
            {
                // Create a feature layer from a portal item.
                ArcGISPortal portal = await ArcGISPortal.CreateAsync(new Uri("https://runtime.maps.arcgis.com/"));
                PortalItem item = await PortalItem.CreateAsync(portal, "b8f4033069f141729ffb298b7418b653");
                var featureLayer = new FeatureLayer(item, 0) { MinScale = 40000000, MaxScale = 4000000 };

                // Load the layer and add it to the map.
                await featureLayer.LoadAsync();
                MyMapView.Map.OperationalLayers.Add(featureLayer);
                VisibilityToggle.IsToggled = featureLayer.IsVisible;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

        private void CheckBox_Checked(object sender, EventArgs e)
        {
            if (MyMapView?.Map?.OperationalLayers?.FirstOrDefault() == null) return;

            MyMapView.Map.OperationalLayers.FirstOrDefault().IsVisible = VisibilityToggle.IsToggled == true;
        }
    }
}

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