Wayfinding Instructions

Android v4

This tutorial will show how to work with the route model returned from a directions service call. It will also show how you can utilize interactions between the route rendering on the map, and text-based instructions showed in another view.

This tutorial will be based off the MapsIndoors Samples example found here: WayFindingarrow-up-right.

An example of the view XML file for the WayfindingFragment this guide will use can be found here: Wayfinding viewarrow-up-right.

First, create variables for MPLocation and MPRoute objects to use later in describing wayfinding. Also create a Variable for MPDirectionsRenderer so we can control rendering through changes in the ViewPager.

WayfindingFragment.ktarrow-up-right

private var mRoute: MPRoute? = null
private var mLocation: MPLocation? = null

Next step we will create the Fragment that will contain a short description of each Leg of the route inside the ViewPager

An example of the view XML file for the RouteLegFragment, that this guide will use, can be found here: RouteLeg viewarrow-up-right.

We will start by adding the code inside the WayfindingFragment.

First, start by changing the code inside onCreateView.

WayfindingFragment.ktarrow-up-right

override fun onCreateView(view: View, @Nullable savedInstanceState: Bundle?) {
    _binding = FragmentWayfindingBinding.inflate(inflater, container, false)

    MapsIndoors.load(requireActivity().applicationContext, "gettingstarted", null)

    val routeLegAdapter = RouteCollectionAdapter(this)
    val viewPager = binding.stepViewPager
    viewPager.adapter = routeLegAdapter
    viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
        }
    })

    val root: View = binding.root
    val supportMapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
    mMapView = supportMapFragment.view
    supportMapFragment.getMapAsync(this)
    return root
}

Next we will create FragmentStateAdapter that will be used on the ViewPager to contain RouteLegFragments

WayfindingFragment.ktarrow-up-right

Let's create a method to textually describe each Leg. This method creates a string that takes the first and last step of the next leg to create a description for the user on what to do at the end of the currently shown leg. You will also create a method to get a list of the different highway types the route can give the user. These are found as enums through the MPHighway class in the MapsIndoors SDK.

WayfindingFragment.ktarrow-up-right

Now, lets create the RouteLegFragment to give context for the Legs in the WayfindingFragment

RouteLegFragment.ktarrow-up-right

You must also update the onViewCreated method to use the new views added earlier in the tutorial.

RouteLegFragment.ktarrow-up-right

Now you have the revised UI for providing the user with a more explanatory route description when navigating. Now it needs to be rendered onto the map.

We will create a method named getRoute that queries a route between two location, we know exist on the current solution. When we receive the route we will assign the route to the fragment as well as calling setRoute on the MPDirectionsRenderer with the received Route. We will also notify the ViewPager that the route is updated with notifyDataSetChanged.

WayfindingFragment.ktarrow-up-right

To change the routing when swapping between tabs on the viewpager, use the call back that we added further up inside the onViewCreated of NavigationFragment.

WayfindingFragment.ktarrow-up-right

You should now have a Fragment with a map that has a route rendered on it. With descriptions of each Leg of the Route inside a ViewPager that will display the Leg related to the page that is viewed.

The full working example can be found here: WayFindingarrow-up-right.

Last updated

Was this helpful?