Control annotation sublayer visibility

View inQMLC++
View on GitHub
Sample viewer app

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

screenshot

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()[i].
  3. You can toggle the visibility of each sublayer manually using sublayer::setVisible().
  4. To determine if a sublayer is visible at the current scale of the MapView, use sublayer::isVisibleAtScale(), by passing in the map's current scale.

Relevant API

  • AnnotationLayer
  • AnnotationSublayer
  • LayerContent

Offline Data

Read more about how to set up the sample's offline data here.

Link Local Location
Gas Device Anno Mobile Map Package <userhome>/ArcGIS/Runtime/Data/mmpk/GasDeviceAnno.mmpk

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.qmlControlAnnotationSublayerVisibility.cppControlAnnotationSublayerVisibility.h
                                                                                                 
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
// [WriteFile Name=ControlAnnotationSublayerVisibility, Category=DisplayInformation]
// [Legal]
// 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.
// [Legal]

import QtQuick 2.6
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.2
import Esri.Samples 1.0

Item {
    // add a mapView component
    MapView {
        id: view
        anchors.fill: parent

        Rectangle {
            id: checkBoxBackground
            anchors {
                left: parent.left
                top: parent.top
                margins: 2
            }
            width: childrenRect.width
            height: childrenRect.height
            color: "white"
            opacity: .75
            radius: 5

            ColumnLayout {
                spacing: 0
                Row {
                    CheckBox {
                        id: openBox
                        checked: true
                        onCheckStateChanged: controlAnnotationSublayerVisibilityModel.openLayerVisible();
                    }

                    Text {
                        id: openBoxText
                        text: controlAnnotationSublayerVisibilityModel.openLayerText
                        anchors.verticalCenter: openBox.verticalCenter
                        color: scale.color
                    }
                }

                Row {
                    CheckBox {
                        id: closedBox
                        checked: true
                        onCheckStateChanged: controlAnnotationSublayerVisibilityModel.closedLayerVisible();
                    }

                    Text {
                        id: closedBoxText
                        text: controlAnnotationSublayerVisibilityModel.closedLayerText
                        anchors.verticalCenter: closedBox.verticalCenter
                    }
                }
            }
        }

        Rectangle {
            id: currentScale
            anchors {
                bottom: view.attributionTop
                horizontalCenter: parent.horizontalCenter
            }
            width: childrenRect.width
            height: childrenRect.height

            Text {
                id: scale
                text: "Current map scale: 1:%1".arg(Math.round(controlAnnotationSublayerVisibilityModel.mapScale))
                color: controlAnnotationSublayerVisibilityModel.visibleAtCurrentExtent ? "black" : "grey"
                padding: 2
            }
        }
    }

    // Declare the C++ instance which creates the scene etc. and supply the view
    ControlAnnotationSublayerVisibilitySample {
        id: controlAnnotationSublayerVisibilityModel
        mapView: view
    }
}

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