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 MMKDrivingRouter class.

    let drivingRouter: MMKDrivingRouter = MMKDirections.sharedInstance().createDrivingRouter(withType: .combined)
    
  2. Set route-building parameters using the MMKDrivingOptions class.

    Example of creating route parameters to request three routes.

    let drivingOptions: MMKDrivingOptions = {
        let options = MMKDrivingOptions()
        options.routesCount = 3
        return options
    }()
    
  3. Use the MMKDrivingVehicleOptions class to define vehicle parameters.

    let vehicleOptions = MMKDrivingVehicleOptions()
    
  4. Create a new route-building session using the MMKDrivingRouter.requestRoutes(with:drivingOptions:vehicleOptions:routeHandler:) method.

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

    let points = [
        MMKRequestPoint(point: MMKPoint(latitude: 25.196141, longitude: 55.278543), type: .waypoint, pointContext: nil, drivingArrivalPointId: nil),
        MMKRequestPoint(point: MMKPoint(latitude: 25.171148, longitude: 55.238034), type: .waypoint, pointContext: nil, drivingArrivalPointId: nil)
    ]
    
    let drivingSession = drivingRouter.requestRoutes(
        with: requestPoints,
        drivingOptions: drivingOptions,
        vehicleOptions: vehicleOptions,
        routeHandler: drivingRouteHandler
    )
    
  5. Use the following mechanism to get the to get the route building results.

    Create an instance of MMKDrivingSessionRouteHandler:

    func drivingRouteHandler(drivingRoutes: [MMKDrivingRoute]?, error: Error?) {
        if let error {
            // Handle request routes error
            return
        }
    
        guard let drivingRoutes else {
            return
        }
    
        // Handle request routes success
    }
    

    Pass this handler method 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 MMKDrivingOptions 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 MMKDrivingVehicleOptions class, you can set the user's type of vehicle. The supported MMKDrivingVehicleType 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 MMKDrivingRouter.requestRoutes(with:drivingOptions:vehicleOptions:routeHandler:) 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 MMKDrivingRouter.requestRoutes(with:drivingOptions:vehicleOptions:routeHandler:) method. The route-building session is represented by the MMKDrivingSession 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 MMKDrivingSession instance it received. Otherwise, the route request will be canceled.

Route results

Use a handler of type MMKDrivingSessionRouteHandler interface to receive the results of route-building requests. Pass this object as a routeHandler parameter when calling the MMKDrivingRouter.requestRoutes(with:drivingOptions:vehicleOptions:routeHandler:) method to create a route-building session.

This handler takes two arguments:

  1. drivingRoutes - says that routes were built, contains the list of resulting routes MMKDrivingRoute.

  2. error - says that routes couldn't be built.

Requests information about routes without geometry

In addition to the route-building functionality, the MMKDrivingRouter 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 MMKDrivingRouter.requestRoutesSummary(with:drivingOptions:vehicleOptions:summaryHandler:) method:

  • Distance.
  • Route duration including and excluding traffic jams.
  • The list of MMKDrivingFlags 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 MMKDrivingSummarySessionSummaryHandler. It's passed as an argument when calling the MMKDrivingRouter.requestRoutesSummary(with:drivingOptions:vehicleOptions:summaryHandler:) method.