Control annotation sublayer visibility

View inAndroidFormsUWPWPFWinUIiOSView on GitHub

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

Image of control annotation sublayer visibility

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 a MobileMapPackage that contains AnnotationSublayer.
  2. Get the sublayers from the map package's layers by calling Sublayer.SublayerContents.
  3. You can toggle the visibility of each sublayer manually using the Sublayer.IsVisible property.
  4. To determine if a sublayer is visible at the current scale of the MapView, use Sublayer.IsVisibleAtScale(MapView.Scale), by passing in the map's current 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

ControlAnnotationSublayerVisibility.xamlControlAnnotationSublayerVisibility.xamlControlAnnotationSublayerVisibility.xaml.cs
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
<ContentPage x:Class="ArcGISRuntimeXamarin.Samples.ControlAnnotationSublayerVisibility.ControlAnnotationSublayerVisibility"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Xamarin.Forms;assembly=Esri.ArcGISRuntime.Xamarin.Forms">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <esriUI:MapView x:Name="MyMapView"
                        Grid.Row="0"
                        Grid.RowSpan="2" />
        <Grid x:Name="StatusGrid"
              Grid.Row="0"
              BackgroundColor="#CC000000">
            <Label x:Name="ScaleLabel"
                   Margin="5"
                   HorizontalOptions="FillAndExpand"
                   HorizontalTextAlignment="Center"
                   Text="Current map scale: 1:"
                   TextColor="White" />
        </Grid>
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Switch x:Name="OpenSwitch"
                    Grid.Column="0"
                    Margin="5"
                    IsEnabled="False"
                    IsToggled="True"
                    Toggled="OpenSwitchChanged"
                    WidthRequest="50" />
            <Label x:Name="OpenLabel"
                   Grid.Column="1"
                   Text="Open"
                   VerticalTextAlignment="Center" />
        </Grid>
        <Grid Grid.Row="3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Switch x:Name="ClosedSwitch"
                    Grid.Column="0"
                    Margin="5"
                    IsEnabled="False"
                    IsToggled="True"
                    Toggled="ClosedSwitchChanged"
                    WidthRequest="50" />
            <Label x:Name="ClosedLabel"
                   Grid.Column="1"
                   Text="Closed"
                   VerticalTextAlignment="Center" />
        </Grid>
        <Label />
    </Grid>
</ContentPage>

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