Browse building floors

View on GitHubSample viewer app

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

BrowseBuildingFloorsApp

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 spinner to browse different floor levels in the facility. Only the selected floor will be displayed.

How it works

  1. Create a PortalItem using the itemId of the floor-aware web map.
  2. Set the MapView to display the PortalItem.
  3. Wait for the map to load and retrieve the map's floor manager from MapView.Map.FloorManager.
  4. Wait for the floor manager to load using FloorManager.load() to retrieve the floor-aware data.
  5. Set all floors to not visible FloorManager.levels[floor-number].isVisible = false.
  6. Set only the selected floor to visible using FloorManager.levels[floor-number].isVisible = true.
  • Note: Manually set the default floor level to the first floor using floorLevel.verticalOrder.
floorManager.levels.first { floorLevel ->  floorLevel.verticalOrder == 0 }

Relevant API

  • FloorManager

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 API also supports browsing different sites and facilities in addition to building floors.

Tags

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

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
/* Copyright 2023 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.arcgismaps.sample.browsebuildingfloors

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.PortalItem
import com.arcgismaps.mapping.floor.FloorLevel
import com.arcgismaps.mapping.floor.FloorManager
import com.arcgismaps.portal.Portal
import com.esri.arcgismaps.sample.browsebuildingfloors.databinding.ActivityMainBinding
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    // set up data binding for the activity
    private val activityMainBinding: ActivityMainBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_main)
    }

    private val mapView by lazy {
        activityMainBinding.mapView
    }

    private val currentFloorTV by lazy {
        activityMainBinding.selectedFloorTV
    }

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

        // authentication with an API key or named user is
        // required to access basemaps and other location services
        ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.API_KEY)
        lifecycle.addObserver(mapView)

        // load the portal and create a map from the portal item
        val portalItem = PortalItem(
            Portal("https://www.arcgis.com/"),
            "f133a698536f44c8884ad81f80b6cfc7"
        )

        // set the map to be displayed in the layout's MapView
        val map = ArcGISMap(portalItem)
        mapView.map = map

        lifecycleScope.launch {
            //load the portal item on the map
            map.load().getOrElse {
                showError("Error loading map" + it.message.toString())
                return@launch
            }

            // load the web map's floor manager
            val floorManager =
                map.floorManager ?: return@launch showError("Map is not floor-aware")
            floorManager.load().getOrElse {
                showError("Error loading floor manager" + it.message.toString())
                return@launch
            }

            // set up dropdown and initial floor level to ground floor
            initializeFloorDropdown(floorManager)
        }
    }

    /**
     * Set and update the floor dropdown. Shows the currently selected floor
     * and hides the other floors using [floorManager].
     */
    private fun initializeFloorDropdown(floorManager: FloorManager) {
        // enable the dropdown view
        activityMainBinding.dropdownMenu.isEnabled = true

        // Select the ground floor using `verticalOrder`.
        // The floor at index 0 might not have a vertical order of 0 if,
        // for example, the building starts with basements.
        // To select the ground floor, we can search for a level with a
        // `verticalOrder` of 0. You can also use level ID, number or name
        // to locate a floor.
        val firstFloorIndex = floorManager.levels.indexOf(
            floorManager.levels.first { it.verticalOrder == 0 }
        )

        currentFloorTV.apply {
            // set the displayed floor to the first floor
            setSelection(firstFloorIndex)

            // set the name of the first floor
            setText(floorManager.levels[firstFloorIndex].longName)

            // set the dropdown adapter for the floor selection
            setAdapter(
                FloorsAdapter(
                    this@MainActivity,
                    android.R.layout.simple_list_item_1,
                    floorManager.levels
                )
            )

            // handle on dropdown item selected
            onItemClickListener =
                AdapterView.OnItemClickListener { _, _, position, _ ->
                    // set all the floors to invisible to reset the floorManager
                    floorManager.levels.forEach { floorLevel ->
                        floorLevel.isVisible = false
                    }

                    // set the currently selected floor to be visible
                    floorManager.levels[position].isVisible = true

                    // set the floor name
                    currentFloorTV.setText(floorManager.levels[position].longName)
                }
        }
    }

    /**
     * Adapter to display a list [floorLevels]
     */
    private class FloorsAdapter(
        context: Context,
        @LayoutRes private val layoutResourceId: Int,
        private val floorLevels: List<FloorLevel>
    ) : ArrayAdapter<FloorLevel>(context, layoutResourceId, floorLevels) {

        private val mLayoutInflater: LayoutInflater =
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

        override fun getCount(): Int {
            return floorLevels.size
        }

        override fun getItem(position: Int): FloorLevel {
            return floorLevels[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            // bind the view to the layout inflater
            val view = convertView ?: mLayoutInflater.inflate(layoutResourceId, parent, false)
            val dropdownItemTV = view.findViewById<TextView>(android.R.id.text1)

            // bind the long name of the floor to it's respective text view
            dropdownItemTV.text = floorLevels[position].longName
            return view
        }
    }

    private fun showError(message: String) {
        Log.e(localClassName, message)
        Snackbar.make(mapView, message, Snackbar.LENGTH_SHORT).show()
    }
}

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