Routing

You can use the MapKit SDK to build optimal routes through waypoints. Routes work around parameters and your vehicle's features.

Warning

Route-building functionality comes with the full version of the MapKit SDK.

Request route building

  1. Create an instance of the DrivingRouter class.

    final drivingRouter = DirectionsFactory.instance.createDrivingRouter(DrivingRouterType.Combined);
    
  2. Set route-building parameters using the DrivingOptions class.

    Example of creating route parameters to request three routes.

    final drivingOptions = DrivingOptions(routesCount: 3);
    
  3. Use the DrivingVehicleOptions class to define vehicle parameters.

    final vehicleOptions = DrivingVehicleOptions();
    
  4. Create a new route-building session using the DrivingRouter.requestRoutes method.

    Example of a request to build a route between two points.

    final points = [
      RequestPoint(Point(latitude: 25.196141, longitude: 55.278543), RequestPointType.Waypoint, null, null)),
      RequestPoint(Point(latitude: 25.171148, longitude: 55.238034), RequestPointType.Waypoint, null, null)),
    ];
    
    final drivingSession = drivingRouter.requestRoutes(
      drivingOptions,
      vehicleOptions,
      drivingRouteListener,
      points: points,
    );
    
  5. Use the following mechanism to get the to get the route building results.

    Create an instance of DrivingSessionRouteListener interface:

    final class DrivingSessionRouteListenerImpl implements DrivingSessionRouteListener {
    
      @override
      void onDrivingRoutes(List<DrivingRoute> routes) {
        // Handle request routes success ...
      }
    
      @override
      void onDrivingRoutesError(Error error) {
        // Handle request routes error ...
      }
    }
    

    Pass an implementation of this interface as an argument whenever you call a method for creating a route-building session.

Route building parameters

You can set the following route-building parameters using the DrivingOptions class:

  • initialAzimuth: Sets the direction of travel at the route starting point to determine whether the user needs to make a U-turn and whether the vehicle is on the with-flow or oncoming lane.
  • routesCount: The number of alternative routes.
  • avoidTolls: Instructs the router to avoid toll roads.
  • avoidPoorConditions: Instructs the router to avoid bad roads where possible.
  • avoidUnpaved: Instructs the router to avoid unpaved roads where possible.
  • departureTime: Departure time.

Vehicle parameters

Using the DrivingVehicleOptions class, you can set the user's type of vehicle. The supported DrivingVehicleType vehicle types are:

  • Standard (passenger car).
  • Truck.
  • Motorcycle.

The route is built based on the selected vehicle's features. Routes may be different for different vehicles. For example, routes for trucks will be adjusted to comply with their restrictions.

Truck restrictions include:

  • Dimensions: height, length, and width.
  • Mass: total mass, suspension weight, maximal allowed weight limit, and so on.
  • Whether the vehicle has a trailer, the vehicle's eco class.

Route-building session

Use the DrivingRouter.requestRoutes method to create a new route-building session. Routes are built based on the list of points passed as the first argument.

There are two types of route points:

  1. Waypoint - used for start and destination points. These include the departure, destination, and intermediate stops.
  2. Viapoint - used to correct the route. The route must pass through all the via points.

A route request session will be created as a result of calling the DrivingRouter.requestRoutes method. The route-building session is represented by the DrivingSession class. Using it, you can manage the status of the route request process via the following methods:

  • retry - relaunch the route-building request.
  • cancel - cancel the current request.

Warning

The app must save the link to the DrivingSession instance it received. Otherwise, the route request will be canceled.

Route results

Use a subscription to the DrivingSessionRouteListener.

Two handler method types are supported:

  1. onDrivingRoutesroutes built, provides the list of resulting routes of type DrivingRoute.

  2. onDrivingRoutesError — notifies that routes couldn't be built.

Requests information about routes without geometry

In addition to the route-building functionality, the DrivingRouter class is also used to retrieve information about routes. This request is a simplified version of the route-building request. As a result, data on route geometry, which consumes the most resources when transmitted over the internet, is not passed. It is used in scenarios where you need to build a route that does not have to be displayed on a map. For example, that might be identifying potential route distances or travel times.

You can get the following information about routes using the DrivingRouter.requestRoutesSummary method:

  • Distance.
  • Route duration including and excluding traffic jams.
  • The list of DrivingFlags that describe the route, including whether there are toll roads, ferry crossings, or parking lots.

You can get the results for route summary requests using the DrivingSummarySessionSummaryListener. It's passed as an argument when calling the DrivingRouter.requestRoutesSummary method.