Skip to content

Handle Speed Limit Alerts

Handle speed limit alerts

This tutorial shows how to handle speed limit alerts.

Get started

All speed limit alerts are zone alerts, representing a zone with a speed limit that is either legal or advisory. For cameras monitoring speed limits, please refer to handle camera alerts.

void AlertObserver::onAlertInfoUpdated(const tn::drive::models::v1::AlertInfo& alertInfo)
{
    const auto& aheadAlerts = alertInfo.alerts_ahead;
    for (const auto& alert : aheadAlerts.speed_limit_alerts)
    {
        // following types are speed limit alerts:
        // tn::drive::models::v1::AlertType::SchoolZone
        // tn::drive::models::v1::AlertType::RainyDaySpeedLimit
        // tn::drive::models::v1::AlertType::SnowyDaySpeedLimit
        // tn::drive::models::v1::AlertType::TimeSpeedLimit
        // tn::drive::models::v1::AlertType::SeasonalTimeSpeedLimit
        // tn::drive::models::v1::AlertType::LaneSpeedLimit
        // tn::drive::models::v1::AlertType::FoggyDaySpeedLimit
        // tn::drive::models::v1::AlertType::OrdinarySpeedLimit
        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;
        }

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

        const auto& speedLimit = alert.speed_limit;
        if ((speedLimit.value != tn::foundation::INVALID_SPEED_LIMIT) &&
            (speedLimit.value != tn::foundation::MAX_SPEED_LIMIT))
        {
            showSpeedLimitIconOnMap(id, speedLimit, alert.zone.begin);
        }
    }
}

Customize school zone detection distance

Some of school zone speed limit alerts come from school sign expansion. School sign is a type of traffic sign indicates driver should drive with caution in around area. It's more of a zone alert instead of a point alert in the case of school sign. However, the affected zone length is usually not available in map data. Therefore, there's a configuration item to make a logical school zone alert with configured length. Besides, schools may locate in near locations, giving multiple school zone alerts with locations close to each other is annoying. There's a configuration item to merge close school zones into one to reduce the number of alerts notified.

const std::string hvaDetectionSettings = R"json(
{
    // Following configuration shows the default values for the school zone detection distance
    "AlertManager": 
    {
        //  This value is the maximum distance for continuous school signs' detection.
        //  The default value is 300m. The unit is meter.
        //  If the distance between two school signs is greater than this value,
        //  they will be output as two separate school zone alerts.
        //  Otherwise, they will be merged into one school zone alert.
        "SignDetectDistance": 300,

        //  This value is the expanded length of a school zone which is converted by a single school sign.
        //  A sign represent the alert info for the next following route, not just for current edge, 
        //  so the alert length might need to be expanded.
        //  The default value is 100. The unit is meter.
        //  If this value is set to 0, the alert length won't be expanded.
        "SingleSignExpandDistance": 100
    }
}
)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 drive session to make customized HVA detection settings effective
tn::drive::DriveSessionOptions options;
options.enable_alert = true;

const auto driveSession = tn::drive::api::DriveSessionFactory::createDriveSession(
    options, system, settings, mapContent, directionService);