Handle Traffic Incident Alerts
Handle traffic incident alerts
This tutorial shows how to handle traffic incident alerts.
Get started
Traffic incident detection is disabled by default.
Enable it with a customized setting at the creation of a navigation service.
| const std::string trafficSettings = R"json(
{
// Following configuration shows the default value for the traffic incident detection
"AlertManager":
{
// EnableTraffic, value: {true, false}.
// Turn on this switch will enable traffic module and related detection modules
"EnableTraffic": false
}
}
)json";
const auto settings = tn::foundation::Settings::Builder()
.setString(tn::drive::api::SettingConstants::SETTING_ALERT_JSON_CONTENT, trafficSettings)
.build();
// enable alert component at the creation of a navigation service to make customized traffic incident detection settings effective
tn::drive::NavigationServiceOptions options;
options.enable_alert = true;
const auto navigationService = tn::drive::api::NavigationServiceFactory::createNavigationService(
options, system, settings, mapContent, directionService);
|
Traffic incident alerts represent both traffic incidents and flows.
For traffic incidents, detailed information will be available.
For traffic flows, all flows that are not free will be notified with estimated time.
| void AlertObserver::onAlertInfoUpdated(const tn::drive::models::v1::AlertInfo& alertInfo)
{
const auto& aheadAlerts = alertInfo.alerts_ahead;
for (const auto& alert : aheadAlerts.traffic_incident_alerts)
{
// following types are traffic alerts:
// tn::drive::models::v1::AlertType::PoliceCheckPoint
// tn::drive::models::v1::AlertType::Accident
// tn::drive::models::v1::AlertType::AccidentCleared
// tn::drive::models::v1::AlertType::RoadConstruction
// tn::drive::models::v1::AlertType::RoadConstructionEnded
// tn::drive::models::v1::AlertType::BlockedRoad
// tn::drive::models::v1::AlertType::HazardousRoad
// tn::drive::models::v1::AlertType::HazardousRoadPassed
// tn::drive::models::v1::AlertType::LaneRestriction
// tn::drive::models::v1::AlertType::LaneRestrictionOver
// tn::drive::models::v1::AlertType::Congestion
// tn::drive::models::v1::AlertType::CongestionCleared
// tn::drive::models::v1::AlertType::EntryBlock
// tn::drive::models::v1::AlertType::EntryReopen
// tn::drive::models::v1::AlertType::ExitBlock
// tn::drive::models::v1::AlertType::ExitReopen
// tn::drive::models::v1::AlertType::TrafficFlow
const auto type = alert.basic.type;
// each alert item has a unique identifier, can be used to match alert updates
const auto& id = alert.basic.id;
if (alert.basic.distance_to_vehicle == 0)
{
// vehicle has entered the zone
// get distance along road between zone begin location and current vehicle location
const auto pastDistance = alert.zone->past_distance;
}
if (type == tn::drive::models::v1::AlertType::TrafficFlow)
{
// for traffic flows
const auto& estimated_time = alert.estimated_time;
showAlertIconOnMap(id, type, alert.zone.begin, alert.zone.end, estimated_time);
}
else
{
// for traffic incidents
showAlertIconOnMap(id, type, alert.zone.begin, alert.zone.end);
const auto& incident = alert.traffic_incident;
if (incident.has_value())
{
// NOTICE: only part of TMC traffic codes are supported
const auto& eventCode = incident->event_code;
if (eventCode.format == tn::mapcontent::models::v1::TrafficIncident::EventCode::Format::Tmc)
{
// handle detailed traffic incident information
// NOTICE: not all traffic incident types defined in
// tn::mapcontent::models::v1::TrafficIncident::Type
// are supported in traffic incident alert detection
const auto type = incident->type;
const auto urgency = incident->urgency;
const auto severity = incident->severity;
// ...
}
}
}
}
}
|