Skip to content

Calculate Route

Calculate Routes

Route Request

Route Style
Route Style Description
Fastest Prefer routes with less travel time. Travel time is the most important factor, but not the only one. This is the only route style that considers live traffic conditions as a main factor. Other styles only consider traffic as a minor factor, so usually only blocking traffic can be avoided.
Shortest Prefer routes with shorter distances. Distance is the most important factor, but not the only one.
Eco Prefer economical routes that use less fuel.
Easy Prefer routes with fewer turns and wider roads.
Route Reference Options

route reference options list avoid items.

Reference Options Description Default
avoid hov road to avoid High-Occupancy Vehicle (HOV) lanes. false
avoid highway to avoid highway roads. false
avoid toll road to avoid toll roads. false
avoid ferry to avoid ferry. false
avoid car train to avoid car train ferry. false
avoid unpaved road to avoid roads those surfaces are not solid (e.g., gravel, dirt or grass). false
avoid tunnel to avoid tunnel. false
avoid traffic congestion to avoid congested roads. true
avoid traffic closures to avoid traffic closure roads strictly.
Note: the routing calculation will treat them as broken networks and it may lead to failures if it's set to true. Otherwize routes including closure roads may be provided if it turns to false
true
avoid country border to avoid crossing country if origin and destination are located in same country. false
avoid sharp turn to avoid sharp turn for safety. especially for big size car. false
avoid roads with permission to avoid roads that entering roads needs permision. true
avoid roads with sesonal restrictions to avoid roads those open or closed affected by seasonl. true
avoid designated road segments to avoid designated road segments false
enable safety route enable route safety calculation which indicates if there is a potential travel risk.
Note: the calculation of route safety is facilitated through an auxiliary cloud service which needs imperative configuration within your project context. Otherwise it will be not available even if this option is enabled.
false
Other Request Options
  • Consider heading and car driving speed: Based on heading and speed, the SDK will avoid the intersection when the system identifies a road that users may not have enough time to react to until a suitable road appears.
  • Support multiple routes calculating in the same route style.
  • Support multiple waypoints.

Create a route request

Request a simple route with origin and destination

A route request is created through the route request builder by providing at least the origin and destination, as shown in the example below .

1
2
3
4
auto request = direction_Service->createRouteRequestBuilder()
                ->setOrigin(tn::direction::models::v2::GeoLocation(37.386300,-122.005090))
                .setDestination(tn::direction::models::v2::GeoLocation(37.398760,-121.977360))
                .build();

Request a route in a specific heading & speed

With the SDK, you can consider the direction a user’s device is facing and request a route starting in a specific direction. To receive a route in a specific direction (for example, the direction a user is traveling or the direction a device is facing), pass in the user’s location heading and speed values. These values can be retrieved from map matching result, please refer to Position Engine section.

1
2
3
4
5
6
auto request = direction_Service->createRouteRequestBuilder()
                ->setOrigin(tn::direction::models::v2::GeoLocation(37.386300,-122.005090))
                .setDestination(tn::direction::models::v2::GeoLocation(37.398760,-121.977360))
                .setHeading(180) // heading to due south
                .setSpeed(5) // with speed 5 m/s
                .build();

Request a route with multiple stops

Besides the final destination, the SDK allows for more stopover locations. If your route involves several pick-up and drop-off points, you can add up to 10 waypoints to the request. These coordinates will be treated as stops between the origin and destination in the order you add them — first comes first.

1
2
3
4
5
6
auto request = direction_Service->createRouteRequestBuilder()
                ->setOrigin(tn::direction::models::v2::GeoLocation(37.386300,-122.005090))
                .setDestination(tn::direction::models::v2::GeoLocation(37.398760,-121.977360))
                .setWaypoints({tn::direction::models::v2::Waypoint(37.398160,-121.977160),
                               tn::direction::models::v2::Waypoint(37.328760,-121.927360)})
                .build();

Request a route with route preferences

A RoutePreference object can also be provided for the request, in order to set customized options.

1
2
3
4
5
6
auto request = direction_Service->createRouteRequestBuilder()
                ->setOrigin(tn::direction::models::v2::GeoLocation(37.386300,-122.005090))
                .setDestination(tn::direction::models::v2::GeoLocation(37.398760,-121.977360))
                .setRoutePreference(
                    tn::direction::models::v2::RoutePreference().avoidHighway(true).avoidTollRoad(true))
                .build();

Calculate a Route

A request is processed through a task, which will finally provide the results through a callback. In the callback, an error code will be provided to indicate the general processing result. Also, a response will be provided with detailed routes if the calculation can be performed properly. For more details, please refer to the corresponding section.

Calulate onboard route

Here is an example of offline route calculation.

1
2
3
4
5
6
7
8
tn::shared_ptr<tn::direction::api::RouteResponse> route_response;
tn::foundation::ErrorCode error_code;

direction_Service->createRouteCalculationTask(request, tn::direction::api::CalculationMode::Onboard)
    ->runSync([&](tn::foundation::ErrorCode c, tn::shared_ptr<tn::direction::api::RouteResponse> r) {
        route_response = r;
        error_code = c;
    });

Calulate offboard route

Before doing the offboard route, you should set the cloud URL,API key, and secret for accessing cloud services. For details, please refer to Set cloud URL when intialization

1
2
3
4
5
6
7
8
tn::shared_ptr<tn::direction::api::RouteResponse> route_response;
tn::foundation::ErrorCode error_code;

direction_Service->createRouteCalculationTask(request, tn::direction::api::CalculationMode::Offboard)
    ->runSync([&](tn::foundation::ErrorCode c, tn::shared_ptr<tn::direction::api::RouteResponse> r) {
        route_response = r;
        error_code = c;
    });