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
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
- Create an
ArcGISMap
with some operational layers. - Set the map on a
MapView
. - Listen to
LayerViewStateChangedEvents
from the map view. - 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 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 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()
}
}