Configuring a menu with AppConfig
- Java
- Kotlin
- iOS v4
AppConfig
contains elements that are useful for any app developer using the SDK. This guide will showcase how to use AppConfig
to create a menu of location categories.
This guide uses code from the "Getting Started" guide. If you have not completed it yet, it is highly recommended to complete it in order to achieve a better understanding of the MapsIndoors SDK. The guide can be found here. If you just want to follow this guide, the getting started code can be found here for java/kotlin.
First, make a new fragment for the menu, and call it MenuFragment
, this fragment will take a list of MPMenuInfo
which will become the elements of the menu.
If you have not followed the getting started guide, the code for
fragment_search_list
can be seen here.
public class MenuFragment extends Fragment {
private List<MPMenuInfo> mMenuInfos = null;
private MapsActivity mMapActivity = null;
public static MenuFragment newInstance(List<MPMenuInfo> menuInfos, MapsActivity mapsActivity) {
final MenuFragment fragment = new MenuFragment();
fragment.mMenuInfos = menuInfos;
fragment.mMapActivity = mapsActivity;
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
// For the brevity of this guide, we will reuse the bottom sheet used in the searchFragment
return inflater.inflate(R.layout.fragment_search_list, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
final RecyclerView recyclerView = (RecyclerView) view;
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new MenuItemAdapter(mMenuInfos, mMapActivity));
}
}
Next make an adapter for this list of MPMenuInfo
. In this example the adapter reuses the ViewHolder
created for the search experience in the getting started guide. For now, Just set the name of the item in onBindViewHolder
, the icon and click listener will be set later.
If you have not followed the getting started guide, the code can be seen here for java/kotlin.
public class MenuItemAdapter extends RecyclerView.Adapter<ViewHolder> {
private final List<MPMenuInfo> mMenuInfos;
private final MapsActivity mMapActivity;
MenuItemAdapter(List<MPMenuInfo> menuInfoList, MapsActivity activity) {
mMenuInfos = menuInfoList;
mMapActivity = activity;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//Setting the the text on the text view to the name of the location
holder.text.setText(mMenuInfos.get(position).getName());
...
}
@Override
public int getItemCount() {
return mMenuInfos.size();
}
}
The icon for the MPMenuInfo
is saved as an URL, as such it has to be downloaded to be displayed. Open a network connection in a background thread and download the image as a stream and save it in a Bitmap
, then update the item's view back on the Main thread.
This is a very rudimentary example, and should not be replicated in production code, see it as an exercise to implement a better way to download and display the icons.
// if there exists an icon for this menuItem, then we will use it
if (mMenuInfos.get(position).getIconUrl() != null) {
// As we need to download the image, it has to be offloaded from the main thread
new Thread(() -> {
Bitmap image;
try {
// Usually we would not want to re-download the image every time, but that is not important for this guide
URL url = new URL(mMenuInfos.get(position).getIconUrl());
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException ignored) {
return;
}
//Set the image while on the main thread
new Handler(Looper.getMainLooper()).post(() -> {
holder.imageView.setImageBitmap(image);
});
}).start();
}
To show which locations belong to the category of the selected MPMenuInfo
, call MapsIndoors.getLocationsAsync(MPQuery, MPFilter, OnLocationsReadyListener)
. The MPQuery
can just be empty as nothing specific needs to be queried. Set the category on the MPFilter
, this has to be the category key MPMenuInfo.getCategoryKey()
, as this key is shared with locations in the SDK.
Finally, in the OnLocationsReadyListener
call MapControl.setFilter(List<MPLocation>, MPFilterBehavior)
as this will filter the map to only show the selected locations.
// When a category is selected, we want to filter the map s.t. it only shows the locations in that
// category
holder.itemView.setOnClickListener(view -> {
// empty query, we do not need to query anything specific
MPQuery query = new MPQuery.Builder().build();
// filter created on the selected category key
MPFilter filter = new MPFilter.Builder().setCategories(Collections.singletonList(mMenuInfos.get(position).getCategoryKey())).build();
MapsIndoors.getLocationsAsync(query, filter, (locations, error) -> {
if (error == null && locations != null) {
mMapActivity.getMapControl().setFilter(locations, MPFilterBehavior.DEFAULT)
}
});
});
Then, in order to show the menu, hijack the search icon's onClickListener
method. The MPMenuInfo
can be fetched via MapsIndoors.getAppConfig().getMenuInfo(String)
, and will give a menu corresponding to the inputted String, in this guide "mainmenu"
has been used as it is the default.
@Override
protected void onCreate(Bundle savedInstanceState) {
...
//ClickListener to start a search, when the user clicks the search button
searchBtn.setOnClickListener(view -> {
/*
if (mSearchTxtField.getText().length() != 0) {
//There is text inside the search field. So lets do the search.
search(mSearchTxtField.getText().toString());
//Making sure keyboard is closed.
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
*/
// Lets hijack the searchbutton
mMenuFragment = MenuFragment.newInstance(MapsIndoors.getAppConfig().getMenuInfo("mainmenu"), this);
//Make a transaction to the bottomsheet
addFragmentToBottomSheet(mMenuFragment);
});
...
}
Finally to clear the category filter from the map call MapControl.clearFilter()
, in this example it is called when closing the menu sheet, it the onDestroyView
method of MenuFragment
.
@Override
public void onDestroyView() {
// When we close the menu fragment we want to display all locations again, not just whichever were selected last
mMapActivity.getMapControl().clearFilter();
super.onDestroyView();
}
The code shown in this guide can be found here for java/kotlin. -->
AppConfig
contains elements that are useful for any app developer using the SDK. This guide will showcase how to use AppConfig
to create a menu of location categories.
This guide uses code from the "Getting Started" guide. If you have not completed it yet, it is highly recommended to complete it in order to achieve a better understanding of the MapsIndoors SDK. The guide can be found here. If you just want to follow this guide, the getting started code can be found here for java/kotlin.
First, make a new fragment for the menu, and call it MenuFragment
, this fragment will take a list of MPMenuInfo
which will become the elements of the menu.
If you have not followed the getting started guide, the code for
fragment_search_list
can be seen here.
class MenuFragment : Fragment() {
private lateinit var mMenuInfos: List<MPMenuInfo?>
private lateinit var mMapActivity: MapsActivity
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// For the brevity of this guide, we will reuse the bottom sheet used in the searchFragment
return inflater.inflate(R.layout.fragment_search, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val recyclerView = view as RecyclerView
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = MenuItemAdapter(mMenuInfos, mMapActivity)
}
companion object {
fun newInstance(menuInfos: List<MPMenuInfo?>, mapsActivity: MapsActivity): MenuFragment {
val fragment = MenuFragment()
fragment.mMenuInfos = menuInfos
fragment.mMapActivity = mapsActivity
return fragment
}
}
}
Next make an adapter for this list of MPMenuInfo
. In this example the adapter reuses the ViewHolder
created for the search experience in the getting started guide. For now, Just set the name of the item in onBindViewHolder
, the icon and click listener will be set later.
If you have not followed the getting started guide, the code can be seen here for java/kotlin.
internal class MenuItemAdapter(
private val mMenuInfos: List<MPMenuInfo?>,
private val mMapActivity: MapsActivity?
) : RecyclerView.Adapter<ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context), parent)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//Setting the the text on the text view to the name of the location
holder.text.text = mMenuInfos[position]?.name
...
}
override fun getItemCount(): Int {
return mMenuInfos.size
}
}
The icon for the MPMenuInfo
is saved as an URL, as such it has to be downloaded to be displayed. Open a network connection in a background thread and download the image as a stream and save it in a Bitmap
, then update the item's view back on the Main thread.
This is a very rudimentary example, and should not be replicated in production code, see it as an exercise to implement a better way to download and display the icons.
// if there exists an icon for this menuItem, then we will use it
val iconUrl = mMenuInfos[position]?.iconUrl
if (iconUrl != null) {
// As we need to download the image, it has to be offloaded from the main thread
Thread {
val image: Bitmap = try {
val url = URL(iconUrl)
BitmapFactory.decodeStream(url.openConnection().getInputStream())
} catch (ignored: IOException) {
return@Thread
}
//Set the image while on the main thread
Handler(Looper.getMainLooper()).post {
holder.imageView.setImageBitmap(
image
)
}
}.start()
}
To show which locations belong to the category of the selected MPMenuInfo
, call MapsIndoors.getLocationsAsync(MPQuery, MPFilter, OnLocationsReadyListener)
. The MPQuery
can just be empty as nothing specific needs to be queried. Set the category on the MPFilter
, this has to be the category key MPMenuInfo.getCategoryKey()
, as this key is shared with locations in the SDK.
Finally, in the OnLocationsReadyListener
call MapControl.setFilter(List<MPLocation>, MPFilterBehavior)
as this will filter the map to only show the selected locations.
// When a category is selected, we want to filter the map s.t. it only shows the locations in that
// category
holder.itemView.setOnClickListener { view ->
// empty query, we do not need to query anything specific
val query = MPQuery.Builder().build()
// filter created on the selected category key
val filter = MPFilter.Builder().setCategories(
listOf(
mMenuInfos[position]?.categoryKey
)
).build()
MapsIndoors.getLocationsAsync(
query, filter
) { locations: List<MPLocation?>?, error: MIError? ->
if (error == null && locations != null) {
mMapActivity?.getMapControl()?.setFilter(locations, MPFilterBehavior.DEFAULT)
}
}
}
Then, in order to show the menu, hijack the search icon's onClickListener
method. The MPMenuInfo
can be fetched via MapsIndoors.getAppConfig().getMenuInfo(String)
, and will give a menu corresponding to the inputted String, in this guide "mainmenu"
has been used as it is the default.
override fun onCreate(savedInstanceState: Bundle?) {
...
//ClickListener to start a search, when the user clicks the search button
var searchBtn = findViewById<ImageButton>(R.id.search_btn)
searchBtn.setOnClickListener {
/*
if (mSearchTxtField.text?.length != 0) {
//There is text inside the search field. So lets do the search.
search(mSearchTxtField.text.toString())
}
//Making sure keyboard is closed.
imm.hideSoftInputFromWindow(it.windowToken, 0)
*/
// Lets hijack the searchbutton
mMenuFragment = MenuFragment.newInstance(MapsIndoors.getAppConfig()?.getMenuInfo("mainmenu")!!, this)
//Make a transaction to the bottomsheet
addFragmentToBottomSheet(mMenuFragment)
}
...
}
Finally to clear the category filter from the map call MapControl.clearFilter()
, in this example it is called when closing the menu sheet, it the onDestroyView
method of MenuFragment
.
override fun onDestroyView() {
// When we close the menu fragment we want to display all locations again, not just whichever were selected last
mMapActivity.getMapControl().clearFilter()
super.onDestroyView()
}
The code shown in this guide can be found here for java/kotlin. -->
AppConfig
contains elements that are useful for any app developer using the SDK. This guide will showcase how to use AppConfig
to create a list of categories and then the connected locations.
The goal is to show the name of the categories, image of the categories and then the location names. By following this guide you can create a working app based on your AppConfig
. This demo app will not show a map or use MPMapControl
.
Import Required Libraries
Begin by importing the necessary libraries at the beginning of your Swift file:
import UIKit
import MapsIndoorsCore
Set Up the AppConfigMenu Class
Define the AppConfigMenu class by inheriting from UIViewController, UITableViewDataSource, and UITableViewDelegate
. This will be needed for making a tableview menu:
class AppConfigMenu: UIViewController, UITableViewDataSource, UITableViewDelegate {
Now, create these instance variables within the class. The variables will be used later:
// Keep an array of the MPMenuInfo, there could be a few...
var menuInfo: [MPMenuInfo] = []
// Keep an array of the categories inside the menuInfo property.
var categories: [String] = []
// Define a tableView
var tableView: UITableView!
// Keep track of the category image URL
var imageUrl: String?
Implement viewDidLoad Method
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: view.bounds, style: .plain)
tableView.dataSource = self
tableView.delegate = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MenuCell")
view.addSubview(tableView)
Task {
try await MPMapsIndoors.shared.load(apiKey: INSERT_API_KEY)
let data = await MPMapsIndoors.shared.appData()
if let mpData = data?.menuInfo {
for (_, value) in mpData {
menuInfo.append(contentsOf: value)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
Implement UITableViewDataSource Methods
Provide data to the table view by implementing the UITableViewDataSource
methods:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuInfo.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell", for: indexPath)
let menuItem = menuInfo[indexPath.row]
cell.textLabel?.text = menuItem.categoryKey
return cell
}
Implement UITableViewDelegate Method
Handle row selection using the UITableViewDelegate method. Keep in mind that the subMenuViewController
will be created in part two. To make the app run without the subMenuViewController
, you can comment out the subMenuViewController
part for now.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedItem = menuInfo[indexPath.row]
if let key = selectedItem.categoryKey {
categories.append(key)
}
if let mpImageUrl = selectedItem.iconUrl {
imageUrl = mpImageUrl
}
let subMenuViewController = SubMenuViewController()
subMenuViewController.menuItem = categories
subMenuViewController.imageUrl = imageUrl
navigationController?.pushViewController(subMenuViewController, animated: true)
}
Implement viewWillDisappear
Implement viewWillDisappear
and make sure the categories array is cleaned when returning to the categories list.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
categories = []
}
You have now successfully integrated the AppConfigMenu class into your iOS application.
This class allows you to display a menu with categories, using the MapsIndoors SDK. Next step is to use AppConfig
to filter MPLocations
on a solution.
Show Locations in a Category
In this part, we will create a new class that handles the sub-list of locations based on your choosen category.
Create a CustomCell class
This class will be used for aligning the image with the cells in the SubMenuViewController
.
class CustomCell: UITableViewCell {
let customImageView: UIImageView = {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(customImageView)
NSLayoutConstraint.activate([
customImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
customImageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
customImageView.widthAnchor.constraint(equalToConstant: 25),
customImageView.heightAnchor.constraint(equalToConstant: 25)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Set Up the SubMenuViewController Class
Define the AppConfigMenu class by inheriting from UIViewController, UITableViewDataSource, and UITableViewDelegate
. This will be needed for making a tableview menu:
class SubMenuViewController: UIViewController, UITableViewDataSource {
Now, create these instance variables within the class.
// menuItem hold that data based on the choosen category from the AppConfigMenu class.
var menuItem: [String]?
// Define a tableView
var tableView: UITableView!
// Hold the filtered locations
var filteredLocations: [MPLocation]? = nil
// Hold the image URL
var imageUrl: String?
Implement viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: view.bounds, style: .plain)
tableView.dataSource = self
tableView.register(CustomCell.self, forCellReuseIdentifier: "LocationMenuCell")
view.addSubview(tableView)
}
Implement data handling
Create a method inside the SubMenuViewController
. This method will load MapsIndoors and fetch locations using a filter of which the category key is used.
func loadAndDisplayData() {
Task {
try await MPMapsIndoors.shared.load(apiKey: INSERT_API_KEY)
// Get all locations under the specific category key using a filter
if let menu = menuItem {
let filter = MPFilter()
filter.categories = menu
filteredLocations = await MPMapsIndoors.shared.locationsWith(query: nil, filter: filter)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
Handle the views
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
filteredLocations = []
// Load data and update the table view
loadAndDisplayData()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
Implement the tableview
These two methods will set the text and images in the cells, using the CustomCell class.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredLocations?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LocationMenuCell", for: indexPath) as! CustomCell
if let myFilteredLocations = filteredLocations {
let locations = myFilteredLocations[indexPath.row]
cell.textLabel?.text = locations.name
getAppConfigImage { image in
DispatchQueue.main.async {
cell.customImageView.image = image
}
}
}
return cell
}
Implement a image data URL request
Create a method inside the SubMenuViewController
that handles the URLSession. This will fetch the image data and make a UIImage baded on that data.
func getAppConfigImage(completion: @escaping (UIImage?) -> Void) {
guard let imageUrl = imageUrl, let url = URL(string: imageUrl) else {
completion(nil)
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data, let image = UIImage(data: data) {
completion(image)
} else {
completion(nil)
}
}.resume()
}
End results


The code shown in this guide can be found here