Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Display layer view state

This code sample is available for these platforms:
View Sample on GitHub

Determine if a layer is currently being viewed.

Image of display layer view state

Use case

The view state includes information on the loading state of layers and whether layers are visible at a given scale. For example, you might change how a layer is displayed in a layer list to communicate whether it is being viewed in the map.

How to use the sample

Pan and zoom around in the map. Each layer's view status is displayed. Notice that some layers configured with a min and max scale change to "OutOfScale" at certain scales.

How it works

  1. Create an Map with some operational layers.
  2. Set the map on a MapView.
  3. Add an event handler for the LayerViewStateChanged event in the map view.

Relevant API

  • LayerViewStateChanged
  • LayerViewStateChangedEventArgs
  • Map
  • MapView

About the data

The map shows a tiled layer of world time zones, a map image layer of the census, and a feature layer of recreation services.

Tags

layer, map, status, view

Sample Code

<UserControl
    x:Class="ArcGISRuntime.UWP.Samples.DisplayLayerViewState.DisplayLayerViewState"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls">
    <Grid>
        <esriUI:MapView x:Name="MyMapView" />
        <Border Background="White" BorderBrush="Black" BorderThickness="1"
                HorizontalAlignment="Right" VerticalAlignment="Top"
                Margin="30" Padding="20" Width="375">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Tiled layer:"
                           Grid.Row="0" Grid.Column="0"
                           Margin="0,5,0,0"
                           HorizontalAlignment="Right" />
                <TextBlock x:Name="TiledLayerStatus"
                           Grid.Row="0" Grid.Column="1"
                           Margin="5,5,0,0"
                           FontWeight="SemiBold" />
                <TextBlock Text="Image layer:"
                           Grid.Row="1" Grid.Column="0"
                           Margin="0,5,0,0"
                           HorizontalAlignment="Right" />
                <TextBlock x:Name="ImageLayerStatus"
                           Grid.Row="1" Grid.Column="1"
                           Margin="5,5,0,0"
                           FontWeight="SemiBold" />
                <TextBlock Text="Feature layer:"
                           Grid.Row="2" Grid.Column="0"
                           Margin="0,5,0,0"
                           HorizontalAlignment="Right" />
                <TextBlock x:Name="FeatureLayerStatus"
                           Grid.Row="2" Grid.Column="1"
                           Margin="5,5,0,0"
                           FontWeight="SemiBold" />
            </Grid>
        </Border>
    </Grid>
</UserControl>
// Copyright 2016 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 System;

namespace ArcGISRuntime.UWP.Samples.DisplayLayerViewState
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Display layer view state",
        category: "MapView",
        description: "Determine if a layer is currently being viewed.",
        instructions: "Pan and zoom around in the map. Each layer's view status is displayed. Notice that some layers configured with a min and max scale change to \"OutOfScale\" at certain scales.",
        tags: new[] { "layer", "map", "status", "view" })]
    public sealed partial class DisplayLayerViewState
    {

        public DisplayLayerViewState()
        {
            InitializeComponent();

            // Create the UI, setup the control references and execute initialization 
            Initialize();
        }

        private void Initialize()
        {
            // Create new Map
            Map myMap = new Map();

            // Create the uri for the tiled layer
            Uri tiledLayerUri = new Uri(
                "https://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer");

            // Create a tiled layer using url
            ArcGISTiledLayer tiledLayer = new ArcGISTiledLayer(tiledLayerUri)
            {
                Name = "Tiled Layer"
            };

            // Add the tiled layer to map
            myMap.OperationalLayers.Add(tiledLayer);

            // Create the uri for the ArcGISMapImage layer
            Uri imageLayerUri = new Uri(
                "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer");

            // Create ArcGISMapImage layer using a url
            ArcGISMapImageLayer imageLayer = new ArcGISMapImageLayer(imageLayerUri)
            {
                Name = "Image Layer",

                // Set the visible scale range for the image layer
                MinScale = 40000000,
                MaxScale = 2000000
            };

            // Add the image layer to map
            myMap.OperationalLayers.Add(imageLayer);

            // Create Uri for feature layer
            Uri featureLayerUri = new Uri(
                "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Recreation/FeatureServer/0");

            // Create a feature layer using url
            FeatureLayer myFeatureLayer = new FeatureLayer(featureLayerUri)
            {
                Name = "Feature Layer"
            };

            // Add the feature layer to map
            myMap.OperationalLayers.Add(myFeatureLayer);

            // Create a map point the map should zoom to
            MapPoint mapPoint = new MapPoint(-11000000, 4500000, SpatialReferences.WebMercator);

            // Set the initial viewpoint for map
            myMap.InitialViewpoint = new Viewpoint(mapPoint, 50000000);

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

            // Provide used Map to the MapView
            MyMapView.Map = myMap;
        }

        private void OnLayerViewStateChanged(object sender, LayerViewStateChangedEventArgs e)
        {
            // For each execution of the MapView.LayerViewStateChanged Event, get the name of
            // the layer and its LayerViewState.Status
            string lName = e.Layer.Name;
            string lViewStatus = e.LayerViewState.Status.ToString();

            // Display the layer name and view status in the appropriate TextBlock control
            switch (lName)
            {
                case "Tiled Layer":
                    TiledLayerStatus.Text = lViewStatus;
                    break;
                case "Image Layer":
                    ImageLayerStatus.Text = lViewStatus;
                    break;
                case "Feature Layer":
                    FeatureLayerStatus.Text = lViewStatus;
                    break;
                default:
                    break;
            }
        }
    }
}