Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Control annotation sublayer visibility

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

Use annotation sublayers to gain finer control of annotation layer subtypes.

Control annotation sublayer visibility App

Use case

Annotation, which differs from labels by having a fixed place and size, is typically only relevant at particular scales. Annotation sublayers allow for finer control of annotation by allowing properties (like visibility in the map and legend) to be set and others to be read (like name) on subtypes of an annotation layer.

An annotation dataset which marks valves as "Opened" or "Closed", might be set to display the "Closed" valves over a broader range of scales than the "Opened" valves, if the "Closed" data is considered more relevant by the map's author. Regardless, the user can be given a manual option to set visibility of annotation sublayers on and off, if required.

How to use the sample

Start the sample and take note of the visibility of the annotation. Zoom in and out to see the annotation turn on and off based on scale ranges set on the data.

Use the checkboxes to manually set "Open" and "Closed" annotation sublayers visibility to on or off.

How it works

  1. Load the MobileMapPackage.
  2. Populate checkbox text with the AnnotationSublayer names (including maximum and minimum scales, where relevant).
  3. Wire up the checkboxes to toggle the annotation sublayer's visibility.
  4. Add a listener for changes in map view navigation and add code to:
    • Update the current scale UI element at the bottom of the screen on navigation.
    • Set the "Open" annotation sublayer checkbox text to:
      • Black, when the layer is drawn at the current map scale
      • Light gray, when the layer is not drawn at the current map scale

Relevant API

  • AnnotationLayer
  • AnnotationSublayer
  • LayerContent

Offline Data

This sample uses Gas Device Anno Mobile Map Package. It is downloaded from ArcGIS Online automatically before the sample runs.

About the data

The scale ranges were set by the map's author using ArcGIS Pro:

  • The "Open" annotation sublayer has its maximum scale set to 1:500 and its minimum scale set to 1:2000.
  • The "Closed" annotation sublayer has no minimum or maximum scales set, so will be drawn at all scales.

Tags

annotation, scale, text, utilities, visualization

Sample Code

<UserControl
    x:Class="ArcGISRuntime.UWP.Samples.ControlAnnotationSublayerVisibility.ControlAnnotationSublayerVisibility"
    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 Style="{StaticResource BorderStyle}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <CheckBox
                    x:Name="OpenCheckBox"
                    Grid.Row="0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    Checked="OpenCheckBoxChanged"
                    IsChecked="True"
                    IsEnabled="False"
                    Unchecked="OpenCheckBoxChanged">
                    <TextBlock x:Name="OpenLabel" Text="Open" />
                </CheckBox>
                <CheckBox
                    x:Name="ClosedCheckBox"
                    Grid.Row="1"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    Checked="ClosedCheckBoxChanged"
                    IsChecked="True"
                    IsEnabled="False"
                    Unchecked="ClosedCheckBoxChanged">
                    <TextBlock x:Name="ClosedLabel" Text="Closed" />
                </CheckBox>
                <TextBlock
                    x:Name="ScaleLabel"
                    Grid.Row="2"
                    HorizontalAlignment="Center"
                    Text="Current map scale: 1:" />
            </Grid>
        </Border>
    </Grid>
</UserControl>
// Copyright 2019 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 ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Mapping;
using System;
using System.Linq;
using Windows.UI;
using Windows.UI.Xaml.Media;

namespace ArcGISRuntime.UWP.Samples.ControlAnnotationSublayerVisibility
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Control annotation sublayer visibility",
        "Layers",
        "Use annotation sublayers to gain finer control of annotation layer subtypes.",
        "")]
    [ArcGISRuntime.Samples.Shared.Attributes.OfflineData("b87307dcfb26411eb2e92e1627cb615b")]
    public partial class ControlAnnotationSublayerVisibility
    {
        // Mobile map package that contains annotation layers.
        private MobileMapPackage _mobileMapPackage;

        // Sub layers of the annotation layer.
        private AnnotationSublayer _openSublayer;
        private AnnotationSublayer _closedSublayer;

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

        private async void Initialize()
        {
            try
            {
                // Load the mobile map package.
                _mobileMapPackage = new MobileMapPackage(DataManager.GetDataFolder("b87307dcfb26411eb2e92e1627cb615b", "GasDeviceAnno.mmpk"));
                await _mobileMapPackage.LoadAsync();

                // Set the mapview to display the map from the package.
                MyMapView.Map = _mobileMapPackage.Maps.First();

                // Get the annotation layer from the MapView operational layers.
                AnnotationLayer annotationLayer = (AnnotationLayer)MyMapView.Map.OperationalLayers.Where(layer => layer is AnnotationLayer).First();

                // Load the annotation layer.
                await annotationLayer.LoadAsync();

                // Get the annotation sub layers.
                _closedSublayer = (AnnotationSublayer)annotationLayer.SublayerContents[0];
                _openSublayer = (AnnotationSublayer)annotationLayer.SublayerContents[1];

                // Set the label content.
                OpenLabel.Text = $"{_openSublayer.Name} (1:{_openSublayer.MaxScale} - 1:{_openSublayer.MinScale})";
                ClosedLabel.Text = _closedSublayer.Name;

                // Enable the check boxes.
                OpenCheckBox.IsEnabled = true;
                ClosedCheckBox.IsEnabled = true;

                // Add event handler for changing the text to indicate whether the "open" sublayer is visible at the current scale.
                MyMapView.ViewpointChanged += (s, e) =>
                {
                    // Check if the sublayer is visible at the current map scale.
                    if (_openSublayer.IsVisibleAtScale(MyMapView.MapScale))
                    {
                        OpenLabel.Foreground = new SolidColorBrush(Colors.Black);
                    }
                    else
                    {
                        OpenLabel.Foreground = new SolidColorBrush(Colors.Gray);
                    }

                    // Set the current map scale text.
                    ScaleLabel.Text = "Current map scale: 1:" + (int)MyMapView.MapScale;
                };
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private void OpenCheckBoxChanged(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            // Set the visibility of the sub layer.
            if (_openSublayer != null) _openSublayer.IsVisible = OpenCheckBox.IsChecked == true;
        }

        private void ClosedCheckBoxChanged(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            // Set the visibility of the sub layer.
            if (_closedSublayer != null) _closedSublayer.IsVisible = ClosedCheckBox.IsChecked == true;
        }
    }
}