Display layer view state

View on GitHubSample viewer app

Determine if a layer is currently being viewed.

Image of display layer view state

Use case

The view status includes information on the loading state of layers and whether layers are visible at a given scale. You might change how a layer is displayed in a layer list to communicate whether it is being viewed in the map. For example, you could show a loading spinner next to its name when the view status is LOADING, grey out the name when NOT_VISIBLE or OUT_OF_SCALE, show the name normally when ACTIVE, or with a warning or error icon when the status is WARNING or ERROR.

How to use the sample

Click "Load Layer" to add a feature layer to the map. The current view status of the layer will display at the top of the sample. Zoom in and out of the map and note the layer disappears when the map is scaled outside of its min and max scale range. Control the layer's visibility with the "Layer visible" checkbox. If you disconnect your device from the network and pan around the map, a warning alert will display. Reconnect to the network to remove the warning. The layer's current view status will update accordingly as you carry out these actions.

How it works

  1. Create an ArcGISMap with some operational layers.
  2. Set the map on a MapView.
  3. Add a LayerViewStateChangedListener to the map view to capture LayerViewStateChangedEvent.
  4. Get the current view status with event.getLayerViewStatus().

Relevant API

  • ArcGISMap
  • LayerViewStateChangedEvent
  • LayerViewStateChangedListener
  • LayerViewStatus
  • MapView

About the data

The Satellite (MODIS) Thermal Hotspots and Fire Activity layer presents detectable thermal activity from MODIS satellites for the last 48 hours. MODIS Global Fires is a product of NASA’s Earth Observing System Data and Information System (EOSDIS), part of NASA's Earth Science Data. EOSDIS integrates remote sensing and GIS technologies to deliver global MODIS hotspot/fire locations to natural resource managers and other stakeholders around the World.

Additional information

The following are members of the LayerViewStatus enum:

  • ACTIVE: The layer in the view is active.
  • NOT_VISIBLE: The layer in the view is not visible.
  • OUT_OF_SCALE: The layer in the view is out of scale. A status of OUT_OF_SCALE indicates that the view is zoomed outside of the scale range of the layer. If the view is zoomed too far in (e.g. to a street level), it is beyond the max scale defined for the layer. If the view has zoomed too far out (e.g. to global scale), it is beyond the min scale defined for the layer.
  • LOADING: The layer in the view is loading. Once loading has completed, the layer will be available for display in the view. If there was a problem loading the layer, the status will be set to ERROR.
  • ERROR: The layer in the view has an unrecoverable error. When the status is ERROR, the layer cannot be rendered in the view. For example, it may have failed to load, be an unsupported layer type, or contain invalid data.
  • WARNING: The layer in the view has a non-breaking problem with its display, such as incomplete information (eg. by requesting more features than the max feature count of a service) or a network request failure.

Tags

layer, load, map, status, view, visibility

Sample Code

DisplayLayerViewStateSample.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright 2017 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.display_layer_view_state;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
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.Paint;
import javafx.stage.Stage;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.ArcGISRuntimeException;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.LayerViewStatus;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.portal.Portal;
import com.esri.arcgisruntime.portal.PortalItem;

public class DisplayLayerViewStateSample extends Application {

  private MapView mapView;
  private FeatureLayer featureLayer;

  @Override
  public void start(Stage stage) {

    try {
      // create the stack pane and the application scene
      StackPane stackPane = new StackPane();
      Scene scene = new Scene(stackPane);
      scene.getStylesheets().add(getClass().getResource("/display_layer_view_state/style.css").toExternalForm());

      // set a title, size, and add the scene to the stage
      stage.setTitle("Display Layer View State Sample");
      stage.setWidth(800);
      stage.setHeight(700);
      stage.setScene(scene);
      stage.show();

      // authentication with an API key or named user is required to access basemaps and other location services
      String yourAPIKey = System.getProperty("apiKey");
      ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);

      // create a map with the topographic basemap style
      ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);

      // create a map view and set the map to it
      mapView = new MapView();
      mapView.setMap(map);

      // set the initial viewpoint for the map view
      mapView.setViewpoint(new Viewpoint(new Point(-11e6, 45e5, SpatialReferences.getWebMercator()), 40000000));

      // create a label to display the view status
      Label layerViewStatusLabel = new Label("Current view status: ");

      // create a checkbox UI that toggles the visibility of the feature layer
      CheckBox visibilityCheckBox = new CheckBox();
      visibilityCheckBox.setText("Layer visible");
      visibilityCheckBox.setSelected(true);
      visibilityCheckBox.setDisable(true);
      visibilityCheckBox.setOnAction(event -> featureLayer.setVisible(visibilityCheckBox.isSelected()));

      // create a button and a listener to load a feature layer
      Button loadLayerButton = new Button("Load Layer");
      loadLayerButton.setOnAction(event -> {

        // disable the checkbox when loading/reloading the feature layer, and, if it exists, remove it by clearing the map's operational layers
        if (!visibilityCheckBox.isDisabled()) {
          visibilityCheckBox.setDisable(true);
        }

        if (featureLayer != null) {
          map.getOperationalLayers().clear();
        }

        // create a new feature layer from a portal item
        final PortalItem portalItem = new PortalItem(new Portal("https://runtime.maps.arcgis.com/"),
          "b8f4033069f141729ffb298b7418b653");
        featureLayer = new FeatureLayer(portalItem, 0);
        //  set a minimum and a maximum scale for the visibility of the feature layer
        featureLayer.setMinScale(40000000);
        featureLayer.setMaxScale(4000000);
        // add the feature layer to the map
        map.getOperationalLayers().add(featureLayer);

        // enable UI interactions once the feature layer has loaded
        featureLayer.addDoneLoadingListener(() -> {
          if (featureLayer.getLoadStatus() == LoadStatus.LOADED) {
            visibilityCheckBox.setDisable(false);
            featureLayer.setVisible(visibilityCheckBox.isSelected());
            loadLayerButton.setText("Reload Layer");
          } else {
            new Alert(Alert.AlertType.ERROR, "Feature layer failed to load").show();
          }
        });
      });

      // create a listener that fires every time a layer's view status changes
      mapView.addLayerViewStateChangedListener(statusChangeEvent -> {

        // check the layer whose state has changed is the feature layer
        if (statusChangeEvent.getLayer() == featureLayer) {

          // get the layer's view status and display the status
          EnumSet<LayerViewStatus> layerViewStatus = statusChangeEvent.getLayerViewStatus();
          List<String> stringList = new ArrayList<>();
          if (layerViewStatus.contains(LayerViewStatus.ACTIVE)) {
            stringList.add("Active");
          }
          if (layerViewStatus.contains(LayerViewStatus.ERROR)) {
            stringList.add("Error");
          }
          if (layerViewStatus.contains(LayerViewStatus.LOADING)) {
            stringList.add("Loading");
          }
          if (layerViewStatus.contains(LayerViewStatus.NOT_VISIBLE)) {
            stringList.add("Not Visible");
          }
          if (layerViewStatus.contains(LayerViewStatus.OUT_OF_SCALE)) {
            stringList.add("Out of Scale");
          }
          if (layerViewStatus.contains(LayerViewStatus.WARNING)) {
            stringList.add("Warning");
          }
          layerViewStatusLabel.setText("Current view status: " + String.join(", ", stringList));

          // show an alert if a warning is detected from the state change
          ArcGISRuntimeException statusError = statusChangeEvent.getError();
          if (statusError != null) {
            new Alert(Alert.AlertType.ERROR, "Unable to update layer view status").show();
          }
        }
      });

      // create a control panel and add the label, checkbox and button UI components
      VBox controlsVBox = new VBox(15);
      controlsVBox.setBackground(new Background(new BackgroundFill(Paint.valueOf("rgba(0,0,0,0.5)"), CornerRadii.EMPTY,
        Insets.EMPTY)));
      controlsVBox.setPadding(new Insets(10.0));
      controlsVBox.setMaxSize(320, 120);
      controlsVBox.getStyleClass().add("panel-region");
      controlsVBox.getChildren().addAll(layerViewStatusLabel, visibilityCheckBox, loadLayerButton);

      // add the map view and control panel to the stack pane
      stackPane.getChildren().addAll(mapView, controlsVBox);
      StackPane.setAlignment(controlsVBox, Pos.TOP_LEFT);
      StackPane.setMargin(controlsVBox, new Insets(10, 0, 0, 10));
    } catch (Exception e) {
      // on any error, display the stack trace
      e.printStackTrace();
    }
  }

  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void stop() {

    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.