Display layer view state

View on GitHub
Sample viewer app

Determine whether a layer is currently visible.

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, gray out the name when notVisible or outOfScale, show the name normally when active, or with an error icon when the state is warning or error.

How to use the sample

When the feature layer is loaded, pan and zoom around the map. Note how the AGSLayerViewState flags change; for example, outOfScale becomes true when the map is scaled outside of the layer's min and max scale range. Tap the toggle to hide the layer and observe the view state change to notVisible.

Disconnect from the network and pan around the map to see the warning status of the different layers when they cannot fetch online data. Reconnect to the network to see the warning disappear.

How it works

  1. Create an AGSMap with some operational layers.
  2. Set the map on an AGSMapView.
  3. Assign a closure to layerViewStateChangedHandler of the map view, which get executed every time a layer's view status changes.
  4. Get the AGSLayer and the current view status of type AGSLayerViewState defining the new state.

Relevant API

  • AGSLayer
  • AGSLayerViewState
  • AGSMap
  • AGSMapView

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 AGSLayerViewState enum:

  • active: The layer in the view is active.
  • notVisible: The layer in the view is not visible.
  • outOfScale: The layer in the view is out of scale. A status of outOfScale 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 (e.g. 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

LayerStatusViewController.swift
                                                                                                                  
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
// Copyright 2015 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.

import UIKit
import ArcGIS

class LayerStatusViewController: UIViewController {
    // MARK: Instance properties and methods

    /// The map view managed by the view controller.
    @IBOutlet weak var mapView: AGSMapView! {
        didSet {
            mapView.map = makeMap(featureLayer: featureLayer)
            mapView.setViewpoint(AGSViewpoint(center: AGSPoint(x: -11e6, y: 45e5, spatialReference: .webMercator()), scale: 2e7))
            mapView.layerViewStateChangedHandler = { [weak self] layer, state in
                guard let self = self else { return }
                // Only check the view state of the feature layer.
                guard layer == self.featureLayer else { return }
                DispatchQueue.main.async {
                    if let error = state.error {
                        self.presentAlert(error: error)
                    }
                    self.setStatus(message: self.viewStatusString(state.status))
                }
            }
        }
    }

    /// The label to display layer view status.
    @IBOutlet weak var statusLabel: UILabel!

    /// The feature layer loaded from a portal item.
    let featureLayer: AGSFeatureLayer = {
        let portalItem = AGSPortalItem(url: URL(string: "https://runtime.maps.arcgis.com/home/item.html?id=b8f4033069f141729ffb298b7418b653")!)!
        let featureLayer = AGSFeatureLayer(item: portalItem, layerID: 0)
        featureLayer.minScale = 1e8
        featureLayer.maxScale = 6e6
        return featureLayer
    }()

    /// Create a map with an `AGSFeatureLayer` added to its operational layers.
    ///
    /// - Parameter featureLayer: An `AGSFeatureLayer` object.
    /// - Returns: An `AGSMap` object.
    func makeMap(featureLayer: AGSFeatureLayer) -> AGSMap {
        let map = AGSMap(basemapStyle: .arcGISTopographic)
        map.operationalLayers.add(featureLayer)
        return map
    }

    // MARK: UI

    func setStatus(message: String) {
        statusLabel.text = message
    }

    /// Get a string for current statuses.
    ///
    /// - Parameter status: An `AGSLayerViewStatus` OptionSet that  indicates the layer's statuses.
    /// - Returns: A comma separated string to represent current statuses.
    func viewStatusString(_ status: AGSLayerViewStatus) -> String {
        var statuses = [String]()
        if status.contains(.active) {
            statuses.append("Active")
        }
        if status.contains(.notVisible) {
            statuses.append("Not Visible")
        }
        if status.contains(.outOfScale) {
            statuses.append("Out of Scale")
        }
        if status.contains(.loading) {
            statuses.append("Loading")
        }
        if status.contains(.error) {
            statuses.append("Error")
        }
        if status.contains(.warning) {
            statuses.append("Warning")
        }
        if !statuses.isEmpty {
            return statuses.joined(separator: ", ")
        } else {
            return "Unknown"
        }
    }

    // MARK: Actions

    @IBAction func layerVisibilitySwitchValueChanged(_ sender: UISwitch) {
        featureLayer.isVisible = sender.isOn
    }

    // MARK: UIViewController

    override func viewDidLoad() {
        super.viewDidLoad()
        // Add the source code button item to the right of navigation bar.
        (self.navigationItem.rightBarButtonItem as? SourceCodeBarButtonItem)?.filenames = ["LayerStatusViewController"]
        // Avoid the overlap between the status label and the map content.
        mapView.contentInset.top = 2 * statusLabel.font.lineHeight
    }
}

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