Determine if a layer is currently being viewed.
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
- Create an
ArcGISMap
with some operational layers. - Set the map on a
MapView
. - Add a
LayerViewStateChangedListener
to the map view to captureLayerViewStateChangedEvent
. - 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 ofOUT_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 isERROR
, 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
/*
* 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);
}
}