Display layer view state

View inJavaKotlin
View on GitHub
Sample 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 NOTVISIBLE or OUTOF_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

Tap the Load layer button to create a new layer and add it to the map. As you pan and zoom around the map, note how the LayerViewStatus flags change; for example, OUT_OF_SCALE becomes true when the map is scaled outside of the layer's min and max scale range. Tap the Hide layer button to hide the layer and observe the view state change to NOT_VISIBLE.

If your device supports airplane mode, you can toggle this on and pan around the map to see layers display the WARNING status when they cannot online fetch data. Toggle airplane mode back off to see the warning disappear.

How it works

  1. Create an ArcGISMap with some operational layers.
  2. Set the map on a MapView.
  3. Listen to LayerViewStateChangedEvents from the map view.
  4. Get the current view status with event.getLayerViewStatus().

Relevant API

  • ArcGISMap
  • LayerViewStateChangedEvent
  • LayerViewStateChangedListener
  • 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

MainActivity.java
                                                                                                                                                                                      
/* Copyright 2016 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.displaylayerviewstate;

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

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
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.layers.Layer;
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 MainActivity extends AppCompatActivity {

  private FeatureLayer mFeatureLayer;
  private MapView mMapView;
  private Button loadButton;
  private View statesContainer;
  private Button hideButton;
  private TextView activeStateTextView;

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

    // authentication with an API key or named user is required to access basemaps and other
    // location services
    ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);

    // inflate MapView from layout
    mMapView = findViewById(R.id.mapView);
    // create a map with the BasemapType topographic
    final ArcGISMap mMap = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // add the map to the map view
    mMapView.setMap(mMap);

    // zoom to custom ViewPoint
    mMapView.setViewpoint(new Viewpoint(new Point(-11000000, 4500000, SpatialReferences.getWebMercator()), 40000000));

    // Listen to changes in the status of the Layer
    mMapView.addLayerViewStateChangedListener(layerViewStateChangedEvent -> {
      // get the layer which changed it's state
      Layer layer = layerViewStateChangedEvent.getLayer();
      // we only want to check the view state of the image layer
      if (!layer.equals(mFeatureLayer)) {
        return;
      }
      // get the View Status of the layer
      // View status will be either of ACTIVE, ERROR, LOADING, NOT_VISIBLE, OUT_OF_SCALE, WARNING
      EnumSet<LayerViewStatus> layerViewStatus = layerViewStateChangedEvent.getLayerViewStatus();
      // if there is an error or warning, display it in a toast
      ArcGISRuntimeException error = layerViewStateChangedEvent.getError();
      if (error != null) {
        Throwable cause = error.getCause();
        String message = cause != null ? cause.toString() : error.toString();
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
      }
      displayViewStateText(layerViewStatus);
    });

    loadButton = findViewById(R.id.loadButton);
    statesContainer = findViewById(R.id.statesContainer);
    hideButton = findViewById(R.id.hideButton);
    activeStateTextView = findViewById(R.id.activeStateTextView);

    loadButton.setOnClickListener(v -> {
      if (mFeatureLayer != null)
        return;
      // load a feature layer from a portal item
      PortalItem portalItem = new PortalItem(new Portal("https://runtime.maps.arcgis.com/"),
          "b8f4033069f141729ffb298b7418b653");

      mFeatureLayer = new FeatureLayer(portalItem, 0);
      // set the scales at which this layer can be viewed
      mFeatureLayer.setMinScale(400_000_000.0);
      mFeatureLayer.setMaxScale(400_000_000.0 / 10);
      // add the layer on the map to load it
      mMap.getOperationalLayers().add(mFeatureLayer);
      // hide the button
      loadButton.setEnabled(false);
      loadButton.setVisibility(View.GONE);
      // show the view state UI and the hide layer button
      statesContainer.setVisibility(View.VISIBLE);
      hideButton.setVisibility(View.VISIBLE);
    });

    hideButton.setOnClickListener(v -> {
      if (mFeatureLayer == null)
        return;

      if (mFeatureLayer.isVisible()) {
        hideButton.setText(R.string.show_layer);
        mFeatureLayer.setVisible(false);
      } else {
        hideButton.setText(R.string.hide_layer);
        mFeatureLayer.setVisible(true);
      }
    });
  }

  /**
   * Formats and displays the layer view status flags in a textview.
   *
   * @param layerViewStatus to display
   */
  protected void displayViewStateText(EnumSet<LayerViewStatus> layerViewStatus) {
    // for each view state property that's active,
    // add it to a list and display the states as a comma-separated string
    List<String> stringList = new ArrayList<>();
    if (layerViewStatus.contains(LayerViewStatus.ACTIVE)) {
      stringList.add(getString(R.string.active_state));
    }
    if (layerViewStatus.contains(LayerViewStatus.ERROR)) {
      stringList.add(getString(R.string.error_state));
    }
    if (layerViewStatus.contains(LayerViewStatus.LOADING)) {
      stringList.add(getString(R.string.loading_state));
    }
    if (layerViewStatus.contains(LayerViewStatus.NOT_VISIBLE)) {
      stringList.add(getString(R.string.not_visible_state));
    }
    if (layerViewStatus.contains(LayerViewStatus.OUT_OF_SCALE)) {
      stringList.add(getString(R.string.out_of_scale_state));
    }
    if (layerViewStatus.contains(LayerViewStatus.WARNING)) {
      stringList.add(getString(R.string.warning_state));
    }
    // join the list of strings with a common and set to display in the text view
    activeStateTextView.setText(TextUtils.join(", ", stringList));
  }

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

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

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