Reroute With Avoidance
Reroute with avoidance
This tutorial shows how to avoid a certain step or traffic incident along the route after navigation is 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 two avoidance preferences 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
In other words, you cannot use a navigation session to change route style, adjust travel points, or change other route preferences.
All these settings used for the calculation of the current route in navigation will be preserved for the avoidance of 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 };
// get a request builder
const auto builder = navigationSession->createRerouteRequestBuilder();
// get a reroute request
const auto request = builder->setAvoidSteps(avoidSteps).setAvoidIncident(avoidIncidents).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();
// get new route
const auto newRoute = response->route();
// accept the reroute result
navigationSession->acceptRerouteResult(response);
}
}
};
|