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 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.kt
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
/*
 * Copyright 2020 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 android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.geometry.Point
import com.esri.arcgisruntime.geometry.SpatialReferences
import com.esri.arcgisruntime.layers.FeatureLayer
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
import com.esri.arcgisruntime.sample.displaylayerviewstate.databinding.ActivityMainBinding
import java.util.EnumSet

class MainActivity : AppCompatActivity() {

    private var featureLayer: FeatureLayer? = null

    private val activityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val mapView: MapView by lazy {
        activityMainBinding.mapView
    }

    private val loadButton: Button by lazy {
        activityMainBinding.loadButton
    }

    private val hideButton: Button by lazy {
        activityMainBinding.hideButton
    }

    private val statesContainer: ConstraintLayout by lazy {
        activityMainBinding.statesContainer
    }

    private val activeStateTextView: TextView by lazy {
        activityMainBinding.activeStateTextView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activityMainBinding.root)

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

        mapView.apply {
            // create a map with a topographic basemap
            map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC)
            // zoom to custom viewpoint
            setViewpoint(
                Viewpoint(
                    Point(-11e6, 45e5, SpatialReferences.getWebMercator()),
                    40_000_000.0
                )
            )
        }

        mapView.addLayerViewStateChangedListener { layerViewStateChangedEvent ->
            // get the layer which changed its state
            val layer = layerViewStateChangedEvent.layer
            // we only want to check the view state of the image layer
            if (layer != featureLayer) {
                return@addLayerViewStateChangedListener
            }

            val layerViewStatus = layerViewStateChangedEvent.layerViewStatus
            // if there is an error or warning, display it in a toast
            layerViewStateChangedEvent.error?.let { error ->
                val message = error.cause?.toString() ?: error.message
                Toast.makeText(this, message, Toast.LENGTH_LONG).show()
            }
            displayViewStateText(layerViewStatus)
        }

        loadButton.setOnClickListener {
            if (featureLayer != null) {
                return@setOnClickListener
            }
            // load a feature layer from a portal item
            val portalItem = PortalItem(
                Portal("https://runtime.maps.arcgis.com/"),
                "b8f4033069f141729ffb298b7418b653"
            )
            featureLayer = FeatureLayer(portalItem, 0).apply {
                // set the scales at which this layer can be viewed
                minScale = 400_000_000.0
                maxScale = minScale / 10
            }
            // add the layer on the map to load it
            mapView.map.operationalLayers.add(featureLayer)
            // hide the button
            loadButton.apply {
                isEnabled = false
                visibility = View.GONE
            }
            // show the view state UI and the hide layer button
            statesContainer.visibility = View.VISIBLE
            hideButton.visibility = View.VISIBLE
        }

        hideButton.setOnClickListener {
            featureLayer?.apply {
                // toggle visibility
                isVisible = !isVisible
                // update button text
                hideButton.text = when {
                    isVisible -> getString(R.string.show_layer)
                    else -> getString(R.string.hide_layer)
                }
            }
        }
    }

    /**
     * Formats and displays the layer view status flags in a text view.
     *
     * @param layerViewStatus to display
     */
    private fun displayViewStateText(layerViewStatus: EnumSet<LayerViewStatus>) {
        // for each view state property that's active,
        // add it to a list and display the states as a comma-separated string
        val stringList = mutableListOf<String>()
        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))
        }
        activeStateTextView.text = stringList.joinToString(", ")
    }

    override fun onResume() {
        super.onResume()
        mapView.resume()
    }

    override fun onPause() {
        mapView.pause()
        super.onPause()
    }

    override fun onDestroy() {
        mapView.dispose()
        super.onDestroy()
    }
}

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