This is an example of creating a simple search experience using MapsIndoors. We will create a map with a search button that leads to another Fragment that handles the search and selection. On selection of a location, we go back to the map and shows the selected location on the map.
First create a Fragment or Activity with a map and MapsIndoors loaded.
We will create a Fragment that will contain a textInput field and a RecyclerView that will show a list of MPLocations received from the search.
classFullscreenSearchFragment : Fragment() {
As we will be using a RecyclerView we will need to create a RecyclerView Adapter to show our Location results. In this guide we will hijack the Adapter from the Template app:
Init and setup the view components to handle searching inside the onViewCreated
searchInputTextView = binding.searchInputEditTextval imm =requireActivity().getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManagersearchInputTextView.addTextChangedListener { searchHandler =Handler(Looper.myLooper()!!) searchHandler!!.postDelayed(searchRunner, 1000)}searchInputTextView.setOnEditorActionListener { textView, i, keyEvent ->if (i == EditorInfo.IME_ACTION_DONE || i == EditorInfo.IME_ACTION_SEARCH) {if (textView.text.isNotEmpty()) {search(textView.text.toString()) }//Making sure keyboard is closed. imm.hideSoftInputFromWindow(textView.windowToken, 0)return@setOnEditorActionListener true }return@setOnEditorActionListener false}
create a Runnable to execute a search
privateval searchRunner: Runnable=Runnable {val text = searchInputTextView.textif (text?.length!!>=2) {search(text.toString()) }}
Add a listener to the Adapter for when a user selects a location, to navigate back to the map and show the selected location. Here we use navigation together with a bundle to tell the other fragment of the selected location
See the sample in FullscreenSearchFragment.kt Now we will implement the FullscreenSearchFragment together with our Fragment or Activity containing a MapsIndoors Map. Add a Button to open the FullscreenSearchFragment inside your Activity or Fragment view and a assign a Click listener to it.
Finally create a way to handle the selected location when a user is navigated to your fragment again. How this example is set up the Map will be reloaded when navigated to it. Therefor we will handle the selection after MapControl is created.
MapControl.create(mapConfig) { mapControl: MapControl?, miError: MIError? -> mMapControl = mapControl//Enable Live Data on the mapif (miError ==null) {var locationId = arguments?.get("locationId") as String?if (locationId !=null) { mMapControl?.selectLocation(locationId, MPSelectionBehavior.DEFAULT) }else {//No errors so getting the first venue (in the white house solution the only one)val venue = MapsIndoors.getVenues()?.defaultVenue activity?.runOnUiThread {if (venue !=null) {//Animates the camera to fit the new venue mMap!!.animateCamera( CameraUpdateFactory.newLatLngBounds( LatLngBoundsConverter.toLatLngBounds(venue.bounds!!),19 ) ) } } } }}