Skip to content

Handle Camera Alerts

Handle camera alerts

This tutorial shows how to handle camera alerts.

Get started

A camera alert can be either a point alert or a zone alert. Check point and zone fields in CameraAlert to see what category it falls into.

void AlertObserver::onAlertInfoUpdated(const tn::drive::models::v1::AlertInfo& alertInfo)
{
    const auto& aheadAlerts = alertInfo.alerts_ahead;
    for (const auto& alert : aheadAlerts.camera_alerts)
    {
        // following types are camera alerts:
        // tn::drive::models::v1::AlertType::SpeedCamera
        // tn::drive::models::v1::AlertType::RedlightCamera
        // tn::drive::models::v1::AlertType::BuslaneCamera
        // tn::drive::models::v1::AlertType::RedlightAndSpeedCamera
        // tn::drive::models::v1::AlertType::SectionStartCamera
        // tn::drive::models::v1::AlertType::SectionEndCamera
        // tn::drive::models::v1::AlertType::DistanceCamera
        // tn::drive::models::v1::AlertType::NoLRTurnsCamera
        // tn::drive::models::v1::AlertType::OtherCamera
        // tn::drive::models::v1::AlertType::BeginningPointOfLaneAndSpeedingViolationBlockCamera
        // tn::drive::models::v1::AlertType::ExclusiveBuslaneAndSpeedingViolationCamera
        // tn::drive::models::v1::AlertType::ExclusiveBuslaneViolationCamera
        // tn::drive::models::v1::AlertType::InformationGatheringCamera
        // tn::drive::models::v1::AlertType::LoadingViolationCamera
        // tn::drive::models::v1::AlertType::NoPassingEnforcementCamera
        // tn::drive::models::v1::AlertType::OtherViolationCamera
        // tn::drive::models::v1::AlertType::OverloadingViolationCamera
        // tn::drive::models::v1::AlertType::ParkingViolationCamera
        // tn::drive::models::v1::AlertType::ShoulderLaneEnforcementCamera
        // tn::drive::models::v1::AlertType::SectionSpeedCamera
        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;

        // some camera has speed limit related
        const auto& speedLimit = alert.camera_info.speed_limit;

        // check if the camera alert is a zone alert
        if (alert.zone.has_value())
        {
            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;
            }

            showAlertIconOnMap(id, type, alert.zone->begin, alert.zone->end);

            if (speedLimit.has_value())
            {
                showSpeedLimitIconOnMap(id, speedLimit.value(), alert.zone->begin);
            }
        }
        // check if the camera alert is a point alert
        else if (alert.point.has_value())
        {
            showAlertIconOnMap(id, type, alert.point->location);

            if (speedLimit.has_value())
            {
                showSpeedLimitIconOnMap(id, speedLimit.value(), alert.point->location);
            }
        }
    }
}

High vigilance area (HVA) alert

It's not legal to give speed camera warning in some countries, in this case, a high vigilance area may be given instead. HVA is not a camera alert but a zone alert.

void AlertObserver::onAlertInfoUpdated(const tn::drive::models::v1::AlertInfo& alertInfo)
{
    const auto& aheadAlerts = alertInfo.alerts_ahead;
    for (const auto& alert : aheadAlerts.zone_alerts)
    {
        if (alert.basic.type == tn::drive::models::v1::AlertType::HighVigilanceArea)
        {
            // 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);
        }
    }
}

When detecting HVA zone, it may only find part of the zone from the begin location due to the complexity of the zone definition. In most cases, the whole distance of HVA zone is much greater than the default alert detection distance. Regarding it is a zone alert, a logical end location should be provided even if the actual end location is out of the detection distance. There's a configuration item to adjust how far should the logical end location for HVA zone be provided.

const std::string hvaDetectionSettings = R"json(
{
    // Following configuration shows the default values for the HVA detection distance
    "AlertManager": 
    {
        //  When detecting HVA zone, we may probably only find part of the zone.
        //  So if we have only the start point in our route, 
        //  we will give a HVA zone from start point that last for "HVAZoneDetectLength" meters.
        "HVAZoneDetectLength": 1000
    }
}
)json";

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

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

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