This guide demonstrates how to populate menus with Mappedin venue data to create a building and level selector. This will allow listing of all buildings, the levels of each building and the ability to switch between them.
Note: A complete class that uses the code snippets in this guide can be found in the Mappedin iOS Github repo: LevelSelectorVC.swift
The populateBuildingMenu
below populates a menu with the names of MPIMapGroups (buildings) used by the buildingButton
. populateBuildingMenu
can be called after the MPIMapViewListener.onDataLoaded method has fired, which indicates the venue data has been loaded and is ready to be used. The UIActions update the buildingButton
title with the chosen MPIMapGroup and call populateLevelMenu to load all of the MPIMap.names (levels) in the levelButton
menu.
// Populates the Building Selection menu with all Map Groups.func populateBuildingMenu() -> UIMenu { var menuActions: [UIAction] = []
// Loop through all map groups and add their name to the building selection menu. mapView?.venueData?.mapGroups.forEach{mapGroup in let buildingAction = UIAction(title: mapGroup.name ?? "Unknown") { (action) in // When a building is selected from the menu, change the button title and trigger loading of all // map names (levels) in the map group into the level selection menu. print(mapGroup.name ?? "Unknown" + " was clicked") self.buildingButton.setTitle(mapGroup.name, for: .normal) self.levelButton.menu = self.populateLevelMenu(selectedBuilding: mapGroup.name ?? "Default") self.mapView?.setMap(map: mapGroup.maps[0]) self.levelButton.setTitle(mapGroup.maps[0].name, for: .normal) } menuActions.append(buildingAction) }
return UIMenu(title: "Choose a Building", options: .displayInline, children: menuActions);}
The populateLevelMenu
is called to populate the menu for the levelButton
. The levelButton
menu must be populated on first load after MPIMapViewListener.onDataLoaded has fired and updated when a new map group is selected.
populateLevelMenu
is called from within the UIActions of each map group (building). The following code shows using UIActions to create a menu for the levelButton
with all MPIMap.names (buildings). When a map name is chosen from the menu, the UIAction fires, updating the levelButton title and loading the chosen map.
// Populates the Level Selection menu with all Map names in the selected building (Map Group).func populateLevelMenu (selectedBuilding:String) -> UIMenu { var menuActions: [UIAction] = []
// Loop through all maps and add their name to the level selection menu. mapView?.venueData?.mapGroups.first(where: {$0.name == selectedBuilding})?.maps.forEach{map in let levelAction = UIAction(title: map.name) { (action) in // When a new level is selected, change the button title and display the chosen map. print(map.name + " was clicked") self.levelButton.setTitle(map.name, for: .normal) self.mapView?.setMap(map: map) } menuActions.append(levelAction) } return UIMenu(title: "Choose a Level", options: .displayInline, children: menuActions);}
Note: A complete class that uses the code snippets in this guide can be found in the Mappedin iOS Github repo: LevelSelectorVC.swift