Directions Service for Android

Last updated:

The class MPDirectionsService 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:

MPDirectionsService directionsService = new MPDirectionsService(mContext);
MPDirectionsRenderer directionsRenderer = new MPDirectionsRenderer(mMapControl);

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

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

});

directionsService.query(origin, destination);
val directionsService = MPDirectionsService(mContext)
val directionsRenderer = MPDirectionsRenderer(mMapControl)

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

directionsService.setRouteResultListener { route, error -> }

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 MPDirectionsService:

void createRoute(MPLocation mpLocation) {
//If MPDirectionsService has not been instantiated create it here and assign the results call back to the activity.
if (mpDirectionsService == null) {
mpDirectionsService = new MPDirectionsService(mContext);
mpDirectionsService.setRouteResultListener(this::onRouteResult);
}
mpDirectionsService.setTravelMode(MPTravelMode.WALKING);
//Queries the MPDirectionsService for a route with the hardcoded user location and the point from a location.
mpDirectionsService.query(mUserLocation, mpLocation.getPoint());
}
fun createRoute(mpLocation: MPLocation) {
//If MPDirectionsService has not been instantiated create it here and assign the results call back to the activity.
if (mpDirectionsService == null) {
mpDirectionsService = MPDirectionsService(mContext)
mpDirectionsService?.setRouteResultListener(this::onRouteResult)
}
mpDirectionsService?.setTravelMode(MPTravelMode.WALKING)
//Queries the MPDirectionsService for a route with the hardcoded user location and the point from a location.
mpDirectionsService?.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 MPDirectionsService.

void getRoute() {
MPDirectionsService directionsService = new MPDirectionsService(mContext);
MPDirectionsRenderer directionsRenderer = new MPDirectionsRenderer(mMapControl);

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

directionsService.addAvoidWayType(MPHighway.STEPS);
directionsService.addAvoidWayType(MPHighway.ELEVATOR);

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

});
directionsService.query(origin, destination);
}
fun getRoute() {
val directionsService = MPDirectionsService(mContext)
val directionsRenderer = MPDirectionsRenderer(mMapControl)

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

directionsService.addAvoidWayType(MPHighway.STEPS)
directionsService.addAvoidWayType(MPHighway.ELEVATOR)

directionsService.setRouteResultListener { route, error -> }

directionsService.query(origin, destination)
}

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

void clearRouteRestrictions() {
mpDirectionsService.clearRouteRestrictions();
}
fun clearRouteRestrictions() {
mpDirectionsService?.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<MPUserRole> getUserRoles() {
return MapsIndoors.getAppliedUserRoles();
}
fun getUserRoles(): List<MPUserRole>? {
return MapsIndoors.getAppliedUserRoles()
}

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

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

This will affect all following Directions requests as well as search queries with MapsIndoors.

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 setTime method on MPDirectionsService and declaring if it is a departure or not through setIsDeparture. 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(Date date) {
mpDirectionsService.setIsDeparture(true);
mpDirectionsService.setTime(date);
}

void setArrivalTime(Date date) {
mpDirectionsService.setIsDeparture(false);
mpDirectionsService.setTime(date);
}
fun setDepartureTime(date: Date?) {
mpDirectionsService.setIsDeparture(true)
mpDirectionsService.setTime(date)
}

fun setArrivalTime(date: Date?) {
mpDirectionsService.setIsDeparture(false)
mpDirectionsService.setTime(date)
}