We define a function search that performs a text search on the name of locations. It creates a MPQuery containing the search string, as well as an empty filter. These are passed to MapsIndoors.getLocationsAsync, which asynchronously returns a list of MPLocations.
constsearch=async (text:string|undefined) => {if (text ===undefined) {console.debug('Clearing search');setSearchResults(undefined);return; }// Do searchconsole.debug(`Searching for "${text}"`);constquery=MPQuery.create({query: text});constfilter=MPFilter.create();constlocations=awaitMapsIndoors.getLocationsAsync(query, filter)setSearchResults(locations); (bottomSheet.current asBottomSheet).expand(); };
We also define a clickResult, which will determine what happens when one of the search results are clicked. In this case we will goTo on MapControl for the location that was clicked. This will pan the map to the location the user clicked, add this inside the MapScreen function.
constclickResult= (result:MPLocation) => {//Centers the map to the chosen MPLocationmapControl?.goTo(result); };
Now for the changes to the rendered layout.
We have added two new Views, one containing the SearchBox component that will represent a search bar on the top of the screen, as well as added the SearchResults component inside the BottomSheet. Notice that the onSearch of the searchBox calls the search function that we implemented. This will set the searchResults populating our searchResults component and expanding the BottomSheet showing the results of the search.
We also add the onClose parameter to the BottomSheet that will clear the previous searchResult when the BottomSheet is closed.
The SearchBox and SearchResults components have already been made, but we need to fill in some data to show our search results from the MPLocation. The SearchBox is just a simple input field with two buttons, to call our search function with a string value from the input box. This can be left as is for now.
First we'll implement the useEffect hook to retrieve an iconUrl from the MPLocation. Here we get the DisplayRule of the location through MapsIndoors and use the IconUrl to display an image in our view for the location.
We will then add two Text elements inside the second view of SearchResultItem to display the name as well as the buildingName of the MPLocation.
You should now be able to run the application and search for locations inside the solution, and be able to click them to show where the location is on the map, as well as read the name and the possible building tied to it.