Browse building floors

View on GitHub
Sample viewer app

Display and browse through building floors from a floor-aware web map.

Browse building floors

Use case

Having map data to aid indoor navigation in buildings with multiple floors such as airports, museums, or offices can be incredibly useful. For example, you may wish to browse through all available floor maps for an office in order to find the location of an upcoming meeting in advance.

How to use the sample

Use the picker to browse different floor levels in the facility. Only the selected floor will be displayed.

How it works

  1. Create and load a floor-aware web map using the identifier of an AGSPortalItem.
  2. Load the map and retrieve the map's floorManager property. Check that the map has a floorManager or floorDefinition property to ensure the map is floor-aware.
  3. Load the floor manager and retrieve the floor-aware data.
  4. Set the current visible floor to the first floor by finding the AGSFloorLevel whose verticalOrder property equals zero.
  5. When an AGSFloorLevel is selected, set only the selected floor level to visible using the isVisible property.

Relevant API

  • AGSFloorLevel
  • AGSFloorManager

About the data

This sample uses a floor-aware web map that displays the floors of Building L on the Esri Redlands campus.

Additional information

The AGSFloorManager API also supports browsing different sites and facilities in addition to building floors.

Floor-awareness APIs support both maps and scenes. To learn more about floor-aware maps, read the Configure floor-aware maps article.

Tags

building, facility, floor, floor-aware, floors, ground floor, indoor, level, site, story

Sample Code

BrowseBuildingFloorsViewController.swift
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
// Copyright 2021 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
//
//   https://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 BrowseBuildingFloorsViewController: UIViewController {
    // MARK: Storyboard views

    /// The map view managed by the view controller.
    @IBOutlet var mapView: AGSMapView! {
        didSet {
            mapView.map = makeMap()
        }
    }
    /// The floor level picker view.
    @IBOutlet var floorLevelPickerView: UIPickerView!

    // MARK: Properties

    /// The floor levels of the floor-aware web map.
    var floorLevels: [AGSFloorLevel] = []

    /// The currently selected floor level.
    var selectedFloorLevel: AGSFloorLevel? {
        didSet {
            // Set the selected level to visible and the previous to invisible.
            oldValue?.isVisible = false
            selectedFloorLevel?.isVisible = true
            // Update picker view selection.
            let row: Int
            if let selectedFloorLevel = selectedFloorLevel {
                row = floorLevels.firstIndex(of: selectedFloorLevel)! + 1
            } else {
                row = .zero
            }
            floorLevelPickerView.selectRow(row, inComponent: 0, animated: true)
        }
    }

    // MARK: Methods

    func setPickerViewLayout() {
        floorLevelPickerView.translatesAutoresizingMaskIntoConstraints = false
        floorLevelPickerView.layer.cornerRadius = 10.0
        NSLayoutConstraint.activate([
            floorLevelPickerView.widthAnchor.constraint(equalToConstant: 120.0),
            view.safeAreaLayoutGuide.trailingAnchor.constraint(equalToSystemSpacingAfter: floorLevelPickerView.trailingAnchor, multiplier: 1),
            mapView.attributionTopAnchor.constraint(equalToSystemSpacingBelow: floorLevelPickerView.bottomAnchor, multiplier: 1)
        ])
    }

    /// Create a map.
    func makeMap() -> AGSMap {
        // A floor-aware web map for floors of Esri Building L in Redlands.
        let map = AGSMap(item: AGSPortalItem(
            portal: .arcGISOnline(withLoginRequired: false),
            itemID: "f133a698536f44c8884ad81f80b6cfc7"
        ))
        map.load { [weak self] error in
            if let error = error {
                self?.presentAlert(error: error)
            } else {
                self?.mapDidLoad(map)
            }
        }
        return map
    }

    /// Called after the web map is loaded without error.
    func mapDidLoad(_ map: AGSMap) {
        // The floor manager of the web map, which exposes the sites,
        // facilities, and levels of the floor-aware data model.
        guard let floorManager = map.floorManager else { return }
        floorManager.load { [weak self] error in
            if let error = error {
                self?.presentAlert(error: error)
            } else {
                self?.floodManagerDidLoad(floorManager)
            }
        }
    }

    /// Called after the floor manager of the web map is loaded without error.
    func floodManagerDidLoad(_ floorManager: AGSFloorManager) {
        guard let geometry = floorManager.sites.first?.geometry,
              // Select the ground floor using `verticalOrder`.
              // In the case of buildings with basements, they can have negative
              // vertical orders; ground floor is expected to be 0.
              // You can also use level ID, number or name to locate a floor.
              let firstFloor = floorManager.levels.first(where: { $0.verticalOrder == 0 }) else { return }
        mapView.setViewpointGeometry(geometry)
        // Update floor levels and select the first floor.
        floorLevels = floorManager.levels
        floorLevelPickerView.reloadAllComponents()
        selectedFloorLevel = firstFloor
    }

    // MARK: UIViewController

    override func viewDidLoad() {
        super.viewDidLoad()
        // Add the source code button item to the right of navigation bar.
        (navigationItem.rightBarButtonItem as? SourceCodeBarButtonItem)?.filenames = ["BrowseBuildingFloorsViewController"]
        // Set the appearance of the floor level picker view.
        setPickerViewLayout()
    }
}

// MARK: - UIPickerViewDataSource

extension BrowseBuildingFloorsViewController: UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        floorLevels.count + 1
    }
}

// MARK: - UIPickerViewDelegate

extension BrowseBuildingFloorsViewController: UIPickerViewDelegate {
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let index = row - 1
        return index >= floorLevels.startIndex ? floorLevels[index].shortName : "None"
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let index = row - 1
        selectedFloorLevel = index >= floorLevels.startIndex ? floorLevels[index] : nil
    }
}

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