Display layer view state

View inJavaKotlinView 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

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
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
/* 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 Basemap Style 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();
  }
}

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