Planning a route

VERSION 1.20.0
PUBLIC PREVIEW

Requesting routes

To calculate a route from A to B, you need to provide route planning criteria. Routes are built using the RoutePlanningOptions class. You can take advantage of the named parameters in Kotlin to choose the properties that you need. The only required parameter is an itinerary. There are multiple optional parameters that you can use to shape the route planning criteria to fit your use cases. For a detailed description of available parameters, see the Routing API documentation.

1val amsterdam = GeoPoint(52.377956, 4.897070)
2val rotterdam = GeoPoint(51.926517, 4.462456)
3val routePlanningOptions = RoutePlanningOptions(
4 itinerary = Itinerary(origin = amsterdam, destination = rotterdam),
5 costModel = CostModel(routeType = RouteType.Efficient),
6 vehicle = Vehicle.Truck(),
7 alternativeRoutesOptions = AlternativeRoutesOptions(maxAlternatives = 2),
8)

Once you have a RoutePlanningOptions object, provide it to the planRoute method. This can be done asynchronously using the RoutePlanningCallback, or synchronously as described in the Synchronous routing call. If the route planning is successful, the onSuccess(result: RoutePlanningResponse) method is called. If an error occurred, it appears in the RoutingFailure.

1routePlanner.planRoute(
2 routePlanningOptions,
3 object : RoutePlanningCallback {
4 override fun onSuccess(result: RoutePlanningResponse) {
5 // YOUR CODE GOES HERE
6 }
7
8 override fun onFailure(failure: RoutingFailure) {
9 // YOUR CODE GOES HERE
10 }
11
12 override fun onRoutePlanned(route: Route) {
13 // YOUR CODE GOES HERE
14 }
15 },
16)
Route with alternatives

Adjusting route planning criteria

Route types

The route type parameter specifies the type of optimization used when calculating routes:

  • Fast: Route calculation is optimized by travel time, while keeping the routes sensible. For example, the calculation may avoid shortcuts along inconvenient side roads or long detours that only save very little time.
  • Short: Route calculation is optimized by travel distance, while keeping the routes sensible. For example, straight routes are preferred over those incurring turns.
  • Efficient: Route calculation is optimized to achieve a good compromise between shorter travel time and lower fuel or energy consumption.
  • Thrilling: Route calculation is optimized so that routes include interesting or challenging roads and use as few motorways as possible.
    • You can choose the level of turns included and also the degree of hilliness. See the hilliness and windingness parameters to set this.
    • There is a limit of 900km on routes planned with thrilling route type.

Default value is set to Fast.

Avoids

The RoutePlanningOptions.costModel.avoidOptions.avoidTypes parameter specifies something that the route calculation should try to avoid when determining the route. The avoid can be specified multiple times. Avoids are honored whenever there is a reasonable alternative available that fulfills all avoid criteria. If the detour becomes too large, some avoid criteria can be violated on parts of the route. The violations can be detected by checking the corresponding route sections.

Possible avoids and their corresponding sections are:

Vehicle

The Vehicle contains parameters relevant for selecting suitable routes. It also provides information about the current state, e.g., the level of fuel. Some roads in the map have vehicle and time dependent restrictions. For example, roads may restrict traffic to pedestrians, or can only be used by electric vehicles. Roads may prohibit vehicles carrying hazardous materials. Tunnels may only be passable by vehicles up to a maximum height, and for trucks with the proper tunnel code.

Vehicles are distinguished by type. Each vehicle type has a different constructor which takes exactly those parameters that are useful for that type. For example, dimensions are only meaningful for motorized vehicles, and not for bikes and pedestrians.

Vehicles have the following properties:

  • A type, e.g. car, motorcycle, truck.
  • A maximum speed.
  • Motorized vehicles can have an engine, either combustion or electric or both for a hybrid vehicle.
    • Pedestrians and bicycles do not support engines.
    • Each engine can contain consumption parameters used to predict the vehicle range.
    • Motorized vehicles may have dimensions.
    • They can be commercial transports.
  • Trucks may have a load for which restrictions apply:
    • A hazmat classification of the load can restrict usable roads.
    • An ADR tunnel code describes which tunnels can be used.

Some vehicle properties are valid only at the current point in time and get updated over time, e.g. the fuel amount/charge level. Vehicle data is also useful during free driving mode without a route, e.g. for range features like 360 range around your current position.

Planning failures

A RoutingFailure is returned if any failure occurred during the Routing API call. There are a few `RoutingFailure`s that are returned in different situations.

Incremental guidance computation

Incremental guidance computation is an optimization technique that reduces route planning time. It works by initially providing the route with limited number of instructions and lane guidance rather than compute all instructions and lane guidance for the entire route.

The mode for guidance computation is controlled by RouteInformationMode. It contains two cases: Complete and FirstIncrement.

  • The Complete case is set by default. In this mode the planRoute provides all instructions for the route immediately.
  • In the FirstIncrement case, the route is generated with the first guidance increment. This takes less time than using Complete mode.
1val amsterdam = GeoPoint(52.377956, 4.897070)
2val rotterdam = GeoPoint(51.926517, 4.462456)
3val routePlanningOptions = RoutePlanningOptions(
4 itinerary = Itinerary(amsterdam, rotterdam),
5 mode = RouteInformationMode.FirstIncrement,
6)
7val result = routePlanner.planRoute(routePlanningOptions)

To get next increment, the increment method advanceGuidanceProgress is used.

1val route = result.routes.first()
2val routeIncrementOptions = RouteIncrementOptions(
3 route,
4 planningOptions = routePlanningOptions,
5)
6val result = routePlanner.advanceGuidanceProgress(routeIncrementOptions)

The advanceGuidanceProgress method is different from planRoute in the following ways:

  • planRoute creates a new route from the options passed as arguments. The computation results in either a route or an error.
  • advanceGuidanceProgress does not create a route. Instead, it works with an existing route, applying the planning options as it extends that route. This method returns either an updated version of existing route with new instructions and an updated guidanceProgressOffset value, or an error if the increment operation failed.

Next steps

Since you have learned how to plan a route, here are recommendations for the next steps: