Reroute
Reroute
This tutorial shows how to avoid a certain step, avoid traffic incidents along the route or change the destination and via points after navigation has started.
Get started
Like regular routing requests you can make with the direction service, the navigation session also provides a way to get a new route during navigation.
The difference is that rerouting in navigation has limited options.
Only the following four items can be set:
- Steps on the current route in navigation to be avoided
- Traffic incidents along the current route in navigation are to be avoided
- Destination
- Via points, in expected arrival sequence
In other words, you cannot use a navigation session to change route style or change other route preferences.
All these settings used for the calculation of the current route in navigation will be preserved for the rerouting in the navigation session.
The process of rerouting in a navigation session is very similar to that of getting a route from a direction service.
Firstly, you build a reroute request from the reroute request builder.
| // for example, get first step of the route to avoid
const auto& legs = route->legs();
const auto& steps = leg[0]->steps();
std::vector<tn:shared_ptr<tn::direction::models::v2::Step>> avoidSteps { steps[0] };
// for example, get first traffic incident along the route to avoid
// traffic incidents are retrieved from navigation status updates
const auto& trafficSignal = navStatus.along_route_traffic_signal;
const auto& incidents = trafficSignal->incidents;
std::vector<tn::traffic::models::v2::TrafficIncident> avoidIncidents { incidents[0].incident };
// change the destination
tn::direction::models::v2::GeoLocation destination {dest_lat, dest_lon};
// edit the waypoints with two stop points for example
tn::direction::models::v2::Waypoint waypoint1 {stop1_lat, stop1_lon};
tn::direction::models::v2::Waypoint waypoint2 {stop2_lat, stop2_lon};
std::vector<tn::direction::models::v2::Waypoint> waypoints {waypoint1, waypoint2};
// get a request builder
const auto builder = navigationSession->createRerouteRequestBuilder();
// get a reroute request
const auto request = builder->setAvoidSteps(avoidSteps)
.setAvoidIncident(avoidIncidents)
.setDestination(destination)
.setWaypoints(waypoints)
.build();
|
Secondly, you create a reroute task with that request.
| // create a reroute task with that request
const auto task = navigationSession->createRerouteTask(request);
// make a response handler
const auto handler = tn::make_shared<RerouteResponseHandler>();
// run the task asynchronously
task->runAsync([handler](tn::foundation::ErrorCode errorCode, tn::shared_ptr<tn::drive::api::RerouteResponse> response){
(void)errorCode; // ignored, it's contained in response
handler->handleResponse(response);
});
|
Thirdly, define a task response handler.
| class RerouteResponseHandler
{
public:
void handleResponse(tn::shared_ptr<tn::drive::api::RerouteResponse> response)
{
const auto status = response->status();
if (status == tn::foundation::SDKError::OK)
{
// get reroute context
const auto currentRoute = response->currentRoute();
const auto rerouteRequest = response->request();
const auto& avoidedSteps = rerouteRequest->avoidSteps();
const auto& avoidedIncidents = rerouteRequest->avoidIncidents();
const auto destination = rerouteRequest->destination();
const auto waypoints = rerouteRequest->waypoints();
// get new route
const auto newRoute = response->route();
// accept the reroute result
navigationSession->acceptRerouteResult(response);
}
}
};
|