Map loaded

View on GitHub
Sample viewer app

Determine the map's load status which can be notLoaded, failedToLoad, loading, or loaded.

Image for Map load status

Use case

Knowing the map's load state may be required before subsequent actions can be executed.

How to use the sample

Open the sample to load the map. The load status will be displayed on screen.

How it works

  1. Create an AGSMap and add it to the AGSMapView.
  2. Use Key-Value Observing on the AGSMap's loadStatus property to determine when the status has changed.

The value of the loadStatus property is loaded when any of the following criteria are met:

  • The map has a valid spatial reference.
  • The map has an an initial viewpoint.
  • One of the map's predefined layers has been created.

Relevant API

  • AGSLoadStatus
  • AGSMap
  • AGSMapView

Tags

loadable pattern, loadStatus, map

Sample Code

MapLoadedViewController.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
// 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.

import UIKit
import ArcGIS

class MapLoadedViewController: UIViewController {
    /// The map displayed in the map view.
    let map = AGSMap(basemapStyle: .arcGISImagery)

    @IBOutlet var mapView: AGSMapView!
    @IBOutlet var bannerLabel: UILabel!

    private var mapLoadStatusObservation: NSKeyValueObservation?

    override func viewDidLoad() {
        super.viewDidLoad()

        // setup source code bar button item
        (navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["MapLoadedViewController"]

        // assign map to map view
        mapView.map = map

        mapLoadStatusObservation = map.observe(\.loadStatus, options: .initial) { [weak self] (_, _) in
            // update the banner label on main thread
            DispatchQueue.main.async {
                self?.updateLoadStatusLabel()
            }
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        updateLoadStatusLabel()
    }

    private func updateLoadStatusLabel() {
        bannerLabel.text = "Load status: \(map.loadStatus.title)"
    }
}

private extension AGSLoadStatus {
    /// The human readable name of the load status.
    var title: String {
        switch self {
        case .loaded:
            return "Loaded"
        case .loading:
            return "Loading"
        case .failedToLoad:
            return "Failed to Load"
        case .notLoaded:
            return "Not Loaded"
        case .unknown:
            fallthrough
        @unknown default:
            return "Unknown"
        }
    }
}

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