Route Navigation
In this guide you will learn how to do real navigation along a computed route rendered on an interactive map, from the current position to a desired destination.
Starting a navigation
Inside MainActivity.ky retain a RoutingService instance.
Normally in Android's MVVM, services are retained in the Repository layer, injected via dependency injection,
and ultimately consumed by ViewModels but for the scope of this example we will use it inside an activity.
The routing service has a NavigationListener
and a ProgressListener
which will update the UI.
The NavigationListener
receives event updates during navigation such as when the destination is reached, or when the route to the desired destination has been recomputed, because a detour away from the original route was taken.
Other events include turn instructions, passed waypoints, voiced instructions, navigation errors and so on.
When the navigation starts the resulting route is shown on map and the follow position button is enabled. The follow position button has a click listener that calls mapView.followPosition()
which will make the camera follow the current position at an angle.
Starting the navigation requiers the user to give permission to access location and location activated. The check for current position is done with PositionService
that uses a PositionListener
get the first available position.
After the first valid position is received navigation is started.