From here onwards, code for both Mapbox and Google Maps is similar.
Take a look at the following code. As discussed before, this will select a location named "Family Dining Room".
Task {do {// Load MapsIndoors with the MapsIndoors API key.tryawait MPMapsIndoors.shared.load(apiKey: YOUR_MAPSINDOORS_API_KEY)iflet mapConfig = mapConfig {iflet mapControl = MPMapsIndoors.createMapControl(mapConfig: mapConfig) {// Retain the mapControl object self.mpMapControl = mapControllet query =MPQuery()let filter =MPFilter() query.query ="Family Dining Room" filter.take =1let locations =await MPMapsIndoors.shared.locationsWith(query: query, filter: filter)iflet firstLocation = locations.first { mapControl.select(location: firstLocation, behavior: .default) mapControl.select(floorIndex: firstLocation.floorIndex.intValue)// set the origin as Family Dining room origin = firstLocation } } } } catch {print("Error loading MapsIndoors: \(error.localizedDescription)") }}
Our goal now is to enable the user to interact with a search bar and move the map with respect to their search. Therefore, we need to implement a bit more functionality into our ViewController class, so feel free to update it as followed.
Let us start off by implementing the search bar. In this case, we add the following variables to our class. The vertical offset of the search bar here is simply to avoid the search bar from colliding with the navigation bar. The tableView will be used to allow the user to see and interact with the search results.
Finally, let us add the functions neccessary for our class to conform to the UITableViewDataSource protocol and the actual search button functionality.
Here, the first 3 functions simply outline the appearence of the table. Namely, how many rows to show and which text to represent each entry.
The main functions of note in this case are, func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) and func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String).
The former denotes what to do once an item is selected, in this case we simply go to the specified location and hide the table view again, while the latter functions almost exactly the same as the initial simple search we included in Display a Map with MapsIndoors.
At this point we should have a functional map with a search feature.