Skip to content

Alert Detection

Alert detection

This tutorial shows how to enable alert detection in navigation service.

Get started

Alert detection requires alert component to be enabled at the creation of a navigation service. A navigation service created with alert component enabled will have alert detection turned on by default. Alerts are detected along the most possible path (MPP) in free mode, in navigation mode it follows the route.

1
2
3
4
5
6
// enable alert component
tn::drive::NavigationServiceOptions options;
options.enable_alert = true;

const auto navigationService = tn::drive::api::NavigationServiceFactory::createNavigationService(
    options, system, settings, mapContent, directionService);

Turn it off if alert detection is not needed. Once turned off, you will not receive any alerts, including cameras, crossing borders, special roads, restrictions, speed limits, tollbooths, traffic signs, or traffic incidents.

// turn off alert detection
navigationService->enableAlertDetection(false);

Alert events listener

Make a customized alert listener to handle alert events. Alerts can be categorized into two types: point alert and zone alert. A point alert indicates there's a road condition affecting a small area. A zone alert indicates there's a road condition affecting a relatively big area.

class AlertObserver : public tn::drive::api::AlertEventListener
{
public:
    void onAlertInfoUpdated(const tn::drive::models::v1::AlertInfo& alertInfo) override
    {
        const auto& aheadAlerts = alertInfo.alerts_ahead;
        for (const auto& alert : aheadAlerts.point_alerts)
        {
            // each alert item has a unique identifier, can be used to match alert updates
            // show an alert icon on map according to the type and location
            showAlertIconOnMap(alert.basic.id, alert.basic.type, alert.point.location);
        }

        for (const auto& alert : aheadAlerts.zone_alerts)
        {
            // each alert item has a unique identifier, can be used to match alert updates
            // show an alert begin and end icon on map according to the type and locations
            showAlertIconOnMap(alert.basic.id, alert.basic.type, alert.zone.begin, alert.zone.end);
        }

        const auto& behindAlerts = alertInfo.alerts_behind;
        for (const auto& alert : behindAlerts.point_alerts)
        {
            // remove passed alert items from map according to previously recorded alert identifiers
            hideAlertIconOnMap(alert.basic.id);
        }

        for (const auto& alert : behindAlerts.zone_alerts)
        {
            // remove passed alert items from map according to previously recorded alert identifiers
            hideAlertIconOnMap(alert.basic.id);
        }
    }

    // omit other methods
};

Register the listener after creation of navigation service.

const auto listener = tn::make_shared<AlertObserver>();
navigationService->addAlertEventListener(listener);

Unregister it when not needed.

navigationService->removeAlertEventListener(listener);

Customize alert detection distance

Alert detection distance can be configured to adjust performance. These detection distances are shared for detection of cameras, administrative information changes, turn and access restrictions, special road conditions, speed limits, tollbooths, traffic incidents and traffic signs. The detection distance should be less than detection distance configured for road graph builder.

const std::string alertDetectionSettings = R"json(
{
    // Following configuration shows the default values for the detection distances
    "AlertManager": 
    {
        //  Alert information retrieve length, unit: meter.
        //  Note that if this value is larger than actual forward detect route length, it will not incur extra road detection.
        "DetectDistance": 5000,

        //  The min distance for detecting alerts in streaming mode, unit: meter.
        //  If the streaming data within this distance is not ready, navigation service will report a data failure to MapContent.
        //  The larger this value is, the more likely the map content mode will be switched to base mode.
        //  Adjust this value with caution.
        "MinDemandDistanceForStreaming": 1000
    }
}
)json";

const auto settings = tn::foundation::Settings::Builder()
    .setString(tn::drive::api::SettingConstants::SETTING_ALERT_JSON_CONTENT, alertDetectionSettings)
    .build();

// enable alert component at the creation of a navigation service to make customized alert detection settings effective
tn::drive::NavigationServiceOptions options;
options.enable_alert = true;

const auto navigationService = tn::drive::api::NavigationServiceFactory::createNavigationService(
    options, system, settings, mapContent, directionService);

Next steps