Directions Service for Android

Last updated:

The class MPRoutingProvider is used to request routes from one point to another. The minimum required input to receive a route is an origin and a destination. This example shows how to setup and execute a query for a Route:

MPRoutingProvider directionsService = new MPRoutingProvider();
MPDirectionsRenderer directionsRenderer = new MPDirectionsRenderer(mContext, mGoogleMap, mMapControl, null);

Point origin = new Point(57.057917, 9.950361, 0.0);
Point destination = new Point(57.058038, 9.950509, 0.0);

directionsService.setOnRouteResultListener((route, error) -> {

});

directionsService.query(origin, destination);
val directionsService = MPRoutingProvider()
val directionsRenderer = MPDirectionsRenderer(this, mMap, mMapControl, null)

val origin = Point(57.057917, 9.950361, 0.0)
val destination = Point(57.058038, 9.950509, 0.0)

directionsService.setOnRouteResultListener { route, miError ->
}

directionsService.query(origin, destination)

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.

Set the travel mode on your request using the setTravelMode method on MPRoutingProvider:

void createRoute(MPLocation mpLocation) {
//If MPRoutingProvider has not been instantiated create it here and assign the results call back to the activity.
if (mpRoutingProvider == null) {
mpRoutingProvider = new MPRoutingProvider();
mpRoutingProvider.setOnRouteResultListener(this);
}
mpRoutingProvider.setTravelMode(TravelMode.WALKING);
//Queries the MPRouting provider for a route with the hardcoded user location and the point from a location.
mpRoutingProvider.query(mUserLocation, mpLocation.getPoint());
}
fun createRoute(mpLocation: MPLocation) {
//If MPRoutingProvider has not been instantiated create it here and assign the results call back to the activity.
if (mpRoutingProvider == null) {
mpRoutingProvider = MPRoutingProvider()
mpRoutingProvider?.setOnRouteResultListener(this)
}
mpRoutingProvider?.setTravelMode(TravelMode.WALKING)
//Queries the MPRouting provider for a route with the hardcoded user location and the point from a location.
mpRoutingProvider?.query(mUserLocation, mpLocation.point)
}

The travel modes generally only apply for outdoor navigation. Indoor navigation calculations are based on the walking travel mode.

Route Restrictions

Avoiding Stairs and Steps

For wheelchair users or other users with limited mobility, it may be relevant to request a Route that avoids stairs and steps. Avoid certain way types on the route using the addRouteRestriction method on MPRoutingProvider.

void getRoute() {
MPRoutingProvider directionsService = new MPRoutingProvider();
MPDirectionsRenderer directionsRenderer = new MPDirectionsRenderer(mContext, mGoogleMap, mMapControl, null);

Point origin = new Point(57.057917, 9.950361, 0.0);
Point destination = new Point(57.058038, 9.950509, 0.0);

directionsService.addRouteRestriction(Highway.STEPS);
directionsService.addRouteRestriction(Highway.ESCALATOR);

directionsService.setOnRouteResultListener((route, error) -> {

});

directionsService.query(origin, destination);
}
fun getRoute() {
val directionsService = MPRoutingProvider()
val directionsRenderer = MPDirectionsRenderer(this, mMap, mMapControl, null)

val origin = Point(57.057917, 9.950361, 0.0)
val destination = Point(57.058038, 9.950509, 0.0)

directionsService.addRouteRestriction(Highway.STEPS)
directionsService.addRouteRestriction(Highway.ESCALATOR)

directionsService.setOnRouteResultListener { route, miError ->
}

directionsService.query(origin, destination)
}

When Route restrictions are set on the MPRoutingProvider they will be applied to any subsequent queries as well. You can remove them again by calling clearRouteRestrictions.

void clearRouteRestrictions() {
mpRoutingProvider.clearRouteRestrictions();
}
fun clearRouteRestrictions() {
mpRoutingProvider?.clearRouteRestrictions()
}

App User Role Restrictions

In the MapsIndoors CMS it is possible to restrict certain ways in the Route Network to only be accessible by users belonging to certain Roles.

You can get the available Roles with help of the MapsIndoors.getAppliedUserRoles:

List<UserRole> getUserRoles() {
return MapsIndoors.getAppliedUserRoles();
}
fun getUserRoles(): List<UserRole>? {
return MapsIndoors.getAppliedUserRoles()
}

User Roles can be set on a global level using MapsIndoors.applyUserRoles.

void setUserRoles(List<UserRole> userRoles) {
MapsIndoors.applyUserRoles(userRoles);
}
fun setUserRoles(userRoles: List<UserRole>) {
MapsIndoors.applyUserRoles(userRoles)
}

This will affect all following Directions requests as well as search queries with MPLocationService. User Roles can also be overwritten on the MPRoutingProvider by using setUserRoles.

void getRoute() {
MPRoutingProvider directionsService = new MPRoutingProvider();
MPDirectionsRenderer directionsRenderer = new MPDirectionsRenderer(mContext, mGoogleMap, mMapControl, null);

Point origin = new Point(57.057917, 9.950361, 0.0);
Point destination = new Point(57.058038, 9.950509, 0.0);

directionsService.setUserRoles(MapsIndoors.getAppliedUserRoles());

directionsService.setOnRouteResultListener((route, error) -> {

});

directionsService.query(origin, destination);
}
fun getRoute() {
val directionsService = MPRoutingProvider()
val directionsRenderer = MPDirectionsRenderer(this, mMap, mMapControl, null)

val origin = Point(57.057917, 9.950361, 0.0)
val destination = Point(57.058038, 9.950509, 0.0)

directionsService.setUserRoles(MapsIndoors.getUserRoles())

directionsService.setOnRouteResultListener { route, miError ->
}
directionsService.query(origin, destination)
}

User Role restrictions set for a specific Directions Query will take precedence over User Roles set on a global level.

For more information about App User Roles, see this documentation.

Transit Departure and Arrival Time

When using the Transit travel mode, you must set a departure date or an arrival date on the route using the setDateTime method on MPRoutingProvider. The date parameter is the epoch time, in seconds, as an integer, and it is only possible to use one of these properties at a time.

void setDepartureTime(int date) {
mpRoutingProvider.setDateTime(date, true);
}

void setArrivalTime(int date) {
mpRoutingProvider.setDateTime(date, false);
}
fun setDepartureTime(date: Int) {
mpRoutingProvider?.setDateTime(date, true)
}

fun setArrivalTime(date: Int) {
mpRoutingProvider?.setDateTime(date, false)
}