Skip to content

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 AnnotationSublayer.
  2. Get the sublayers from the map package's layers by calling Layer.getSubLayerContents.get(i).
  3. You can toggle the visibility of each sublayer manually using .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

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

ControlAnnotationSublayerVisibilitySample.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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 /*  * 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.samples.control_annotation_sublayer_visibility;  import javafx.application.Application; import javafx.beans.binding.Bindings; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.CornerRadii; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.paint.Paint; import javafx.stage.Stage;  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;  import java.io.File;  public class ControlAnnotationSublayerVisibilitySample extends Application {   private MapView mapView;  private MobileMapPackage mobileMapPackage;   @Override  public void start(Stage stage) {   try {  // create stack pane and application scene  StackPane stackPane = new StackPane();  Scene scene = new Scene(stackPane);  scene.getStylesheets().add(getClass().getResource("/control_annotation_sublayer_visibility/style.css").toExternalForm());   // set title, size, and add scene to stage  stage.setTitle("Control Annotation Sublayer Visibility");  stage.setWidth(800);  stage.setHeight(700);  stage.setScene(scene);  stage.show();   // create a map view  mapView = new MapView();   // create checkboxes for toggling the sublayer visibility manually  CheckBox closedSublayerCheckbox = new CheckBox();  closedSublayerCheckbox.setSelected(true);  closedSublayerCheckbox.setTextFill(Color.WHITE);  CheckBox openSublayerCheckbox = new CheckBox();  openSublayerCheckbox.setSelected(true);  openSublayerCheckbox.setTextFill(Color.WHITE);   // create a control panel and label for the checkboxes  VBox controlsVBox = new VBox(6);  controlsVBox.getStyleClass().add("panel-region");  controlsVBox.setBackground(new Background(new BackgroundFill(Paint.valueOf("rgba(0,0,0,0.3)"), CornerRadii.EMPTY,  Insets.EMPTY)));  controlsVBox.setPadding(new Insets(10.0));  controlsVBox.setMaxSize(180, 85);   // show current map scale in a label within the control panel  Label currentMapScaleLabel = new Label();   // listen for map scale changes and update the label  mapView.mapScaleProperty().addListener((observable, oldValue, newValue) ->  currentMapScaleLabel.setText("Scale: 1:" + Math.round((double) newValue)));   // add checkboxes and label to the control panel  controlsVBox.getChildren().addAll(closedSublayerCheckbox, openSublayerCheckbox, currentMapScaleLabel);   // load the mobile map package  File mmpkFile = new File(System.getProperty("data.dir"), "./samples-data/mmpk/GasDeviceAnno.mmpk");  mobileMapPackage = new MobileMapPackage(mmpkFile.getAbsolutePath());  mobileMapPackage.loadAsync();  mobileMapPackage.addDoneLoadingListener(() -> {  if (mobileMapPackage.getLoadStatus() == LoadStatus.LOADED && !mobileMapPackage.getMaps().isEmpty()) {  // set the mobile map package's map to the map view  mapView.setMap(mobileMapPackage.getMaps().get(0));  // find the annotation layer within the map  for (Layer layer : mapView.getMap().getOperationalLayers()) {  if (layer instanceof AnnotationLayer) {  // load the annotation layer  layer.loadAsync();  layer.addDoneLoadingListener(() -> {  if (layer.getLoadStatus() == LoadStatus.LOADED) {  // get annotation sublayer name from sublayer content  AnnotationSublayer closedSublayer = (AnnotationSublayer) layer.getSubLayerContents().get(0);  AnnotationSublayer openSublayer = (AnnotationSublayer) layer.getSubLayerContents().get(1);   // set the layer name for the checkboxes  closedSublayerCheckbox.setText(closedSublayer.getName());  openSublayerCheckbox.setText(String.format("%s (1:%d - 1:%d)", openSublayer.getName(), Math.round(openSublayer.getMaxScale()), Math.round(openSublayer.getMinScale())));   // toggle annotation sublayer visibility on check  closedSublayerCheckbox.setOnAction(event -> closedSublayer.setVisible(closedSublayerCheckbox.isSelected()));  openSublayerCheckbox.setOnAction(event -> openSublayer.setVisible(openSublayerCheckbox.isSelected()));   // bind the checkbox disable property to its map scale visibility  var mapScaleProperty = mapView.mapScaleProperty();  openSublayerCheckbox.disableProperty().bind(Bindings.createBooleanBinding(() ->  !openSublayer.isVisibleAtScale(mapScaleProperty.get()), mapScaleProperty));   } else {  new Alert(Alert.AlertType.ERROR, "Error loading Annotation Layer " + layer.getName()).show();  }  });  }  }   } else {  new Alert(Alert.AlertType.ERROR, "Mobile Map Package failed to load.").show();  }  });   // add map view and label to stack pane  stackPane.getChildren().addAll(mapView, controlsVBox);  StackPane.setAlignment(controlsVBox, Pos.TOP_LEFT);  StackPane.setMargin(controlsVBox, new Insets(20, 0, 0, 20));   } catch (Exception e) {  // on any error, print the stack trace  e.printStackTrace();  }  }   /**  * Stops and releases all resources used in application.  */  @Override  public void stop() {   // release resources when the application closes  if (mapView != null) {  mapView.dispose();  }  }   /**  * Opens and runs application.  *  * @param args arguments passed to this application  */  public static void main(String[] args) {   Application.launch(args);  }  }

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