Control annotation sublayer visibility

View on GitHubSample viewer app

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 an AnnotationSublayer.
  2. Get the sublayers from the map package's layers by calling sublayer.getSubLayerContents.get(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

  1. Download the data from ArcGIS Online.
  2. Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
  3. Push the data into the scoped storage of the sample app:

adb push GasDeviceAnno.mmpk /Android/data/com.esri.arcgisruntime.sample.controlannotationsublayervisibility/files/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

MainActivity.java
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 *  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.
 */

package com.esri.arcgisruntime.sample.annotationsublayer;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.layers.AnnotationLayer;
import com.esri.arcgisruntime.layers.AnnotationSublayer;
import com.esri.arcgisruntime.layers.Layer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.MobileMapPackage;
import com.esri.arcgisruntime.mapping.view.MapView;

public class MainActivity extends AppCompatActivity {

  private static final String TAG = MainActivity.class.getSimpleName();

  private MapView mMapView;
  // objects that implement Loadable must be class fields to prevent being garbage collected before loading
  private MobileMapPackage mMobileMapPackage;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // get a reference to the map view
    mMapView = findViewById(R.id.mapView);

    // show current map scale in a text view at the bottom of the screen
    TextView currentMapScaleTextView = findViewById(R.id.mapScale);
    mMapView.addMapScaleChangedListener(mapScaleChangedEvent -> currentMapScaleTextView
        .setText(getString(R.string.map_scale, Math.round(mMapView.getMapScale()))));

    // get a reference to checkboxes
    CheckBox closedCheckBox = findViewById(R.id.closedCheckBox);
    CheckBox openCheckBox = findViewById(R.id.openCheckBox);

    // load the mobile map package
    mMobileMapPackage = new MobileMapPackage(getExternalFilesDir(null) + getString(R.string.gas_device_anno_mmpk_path));
    mMobileMapPackage.loadAsync();
    mMobileMapPackage.addDoneLoadingListener(() -> {
      if (mMobileMapPackage.getLoadStatus() == LoadStatus.LOADED) {
        // set the mobile map package's map to the map view
        mMapView.setMap(mMobileMapPackage.getMaps().get(0));
        // find the annotation layer within the map
        for (Layer layer : mMapView.getMap().getOperationalLayers()) {
          if (layer instanceof AnnotationLayer) {
            // load the annotation layer. The layer must be loaded in order to access sub-layer contents
            layer.loadAsync();
            layer.addDoneLoadingListener(() -> {
              // get annotation sublayer name from sublayer contents
              AnnotationSublayer closedLayer = (AnnotationSublayer) layer.getSubLayerContents().get(0);
              AnnotationSublayer openLayer = (AnnotationSublayer) layer.getSubLayerContents().get(1);

              // set the layer name from the
              closedCheckBox.setText(buildLayerName(closedLayer));
              openCheckBox.setText(buildLayerName(openLayer));

              // toggle annotation sublayer visibility on check
              closedCheckBox.setOnCheckedChangeListener(
                  (checkBoxView, isChecked) -> closedLayer.setVisible(isChecked));
              openCheckBox.setOnCheckedChangeListener(
                  (checkBoxView, isChecked) -> openLayer.setVisible(isChecked));

              // when the map scale changes
              mMapView.addMapScaleChangedListener(mapScaleChangedEvent -> {
                // if the "open" layer is visible, set text color to black, otherwise set it to gray
                if (openLayer.isVisibleAtScale(mMapView.getMapScale())) {
                  openCheckBox.setTextColor(Color.BLACK);
                } else {
                  openCheckBox.setTextColor(Color.LTGRAY);
                }
              });

            });
          }
        }
      } else {
        String error = "Mobile map package failed load: " + mMobileMapPackage.getLoadError().getMessage();
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
        Log.e(TAG, error);
      }
    });
  }

  /**
   * Get name, and where relevant, append min and max scales of each annotation sublayer.
   *
   * @param annotationSublayer
   * @return the layer name with min max scales, where relevant
   */
  private String buildLayerName(AnnotationSublayer annotationSublayer) {
    StringBuilder layerNameBuilder = new StringBuilder(annotationSublayer.getName());
    if (!Double.isNaN(annotationSublayer.getMaxScale()) && !Double.isNaN(annotationSublayer.getMinScale())) {
      layerNameBuilder.append(" (1:").append((int) annotationSublayer.getMaxScale()).append(" - 1:")
          .append((int) annotationSublayer.getMinScale()).append(")");
    }
    return layerNameBuilder.toString();
  }

  @Override
  protected void onPause() {
    mMapView.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override
  protected void onDestroy() {
    mMapView.dispose();
    super.onDestroy();
  }
}

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