Getting Directions
Now we have a simple map with a floor selector where you can search for locations. When finishing this step you'll be able to create a directions between two points and change the transportation mode.
Get Directions Between Two Locations
After having created our list of search results, we have a good starting point for creating directions between two Locations. Since our search only supports a single search, we will hardcode a Location's coordinate into our app, and use that as the basis for our Origin. Then we'll create a route, navigate to a view of the navigation details, and show a route on the map from the Origin to the Destination.
We have already created a point in the basic example, called mUserLocation
to use as a starting point for directions on MapsActivity
Now we will create a method that can generate a route for us with a Location (picked from the search list). Start by implementing OnRouteResultListener
to your MapsActivity.
Implement the onRouteResult
method and create a method called createRoute(MPLocation mpLocation)
on your MapsActivity
.
Use this method to query the MPDirectionsService
, which generates a route between two coordinates. We will use this to query a route with our hardcoded mUserLocation
and a point from a MPLocation.
To generate a route with the MPLocation
, we start by creating an onClickListener
on our search ViewHolder
inside the SearchItemAdapter
. In the method onBindViewHolder
we will call our createRoute
on the MapsActivity
for our route to be generated.
We start by implementing logic to our createRoute method to query a route through MPDirectionsService
and assign the onRouteResultListener to the activity. When we call the createRoute
through our onClickListener
we will receive a result through our onRouteResult
implementation.
When we receive a result on our listener, we render the route through the MPDirectionsRenderer
.
We create global variables of the MPdirectionsRenderer
and MPDirectionsService
and create a getter to the MPdirectionsRenderer
to access it from fragments later on.
See the full implementation of these methods here: MapsActivity.java or MapsActivity.kt
Now we will implement logic to our NavigationFragment
that we can put into our BottomSheet and show the steps for each route, as well as the time and distance it takes to travel the route.
Here we'll use a viewpager
to allow the user to switch between each step, as well as display a "close" button so we are able to remove the route and the bottom sheet from the activity.
We will start by making a getter for our MPdirectionsRenderer
that we store on MapsActivity
:
Inside the NavigationFragment
we will implement logic to navigate through Legs of our Route.
See the full implementation of NavigationFragment
and the accompanying adapter here: NavigationFragment.java or NavigationFragment.kt
We will then create a simple textview to describe each step of the Route Leg in the RouteLegFragment
for the ViewPager
:
See the full implementation of the fragment here: RouteLegFragment.java or RouteLegFragment.kt
Change Transportation Mode
In MapsIndoors, the transportation mode is referred to as travel mode. There are four travel modes, walking, bicycling, driving and transit (public transportation). The travel modes generally applies for outdoor navigation. Indoor navigation calculations are based on walking travel mode.
To swap Travel Modes you set the Travel Mode before making a query for the route:
Expected result:
Last updated