Skip to content

场景示例

以下示例分别覆盖局部 Lane Group 查询、路线关联与前路结果、Lane Group 交通。示例函数假设 HDMapService 已完成创建;服务初始化和配置见快速开始

场景 前置条件 主要结果
查询局部 Lane Group 地图版本与目标区域数据可用 Lane Group、Lane 与车道边界线摘要
消费路线关联与前路结果 通信模块持续提供车辆上下文与路线上下文 已匹配路线道路段对应的 Lane Group
查询 Lane Group 交通 通信模块提供交通数据,且请求 Lane Group 可以完成关联 事件、流量或明确的空结果状态

查询局部 Lane Group

先用局部坐标范围获取 LaneGroupId,再读取每个 Lane Group 的详情。空间查询和详情查询分别检查 Status,每次调用使用独立输出对象。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void inspectLocalLaneGroups(tn::hdmap::HDMapService& service,
                            const tn::hdmap::models::LatLon& center)
{
    std::vector<tn::hdmap::models::LaneGroupId> ids;
    const auto fetch_status = service.fetchLaneGroups(ids, center, 100);
    if (fetch_status != tn::hdmap::Status::OK)
    {
        std::cout << "Lane Group query status="
                  << static_cast<int>(fetch_status) << '\n';
        return;
    }

    for (const auto id : ids)
    {
        tn::hdmap::models::LaneGroup group;
        if (service.queryLaneGroupInfo(group, id) != tn::hdmap::Status::OK)
        {
            continue;
        }

        std::cout << "LaneGroup " << group.id
                  << ", lanes=" << group.lanes.size() << '\n';
        for (const auto& lane : group.lanes)
        {
            std::cout << "  Lane " << lane.id
                      << ", center points=" << lane.geometry.size()
                      << ", left points=" << lane.left.geometry.size()
                      << ", right points=" << lane.right.geometry.size()
                      << '\n';
        }
    }
}

fetchLaneGroups(...) 返回 OKids 为空时,本次调用成功但没有返回 Lane Group ID;应用进入无结果路径,并结合交付覆盖清单、验证坐标和实际地图版本定位原因。DataPending 可以在业务截止时间内有限重试;DataNotFound 进入无数据路径。

消费路线关联与前路结果

回调先检查通信可用性、导航系统输入心跳、导航状态和偏航状态,再消费已匹配路线道路段中的 Lane Group。停止使用该能力时传入空回调解除监听。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
tn::hdmap::Status observeAheadRoad(tn::hdmap::HDMapService& service)
{
    using NavigationStatus = tn::hdmap::models::NavigationStatus;
    return service.setNavigationStatusListener(
        [](const NavigationStatus& status)
        {
            if (!status.is_connected_to_navigation_sdk ||
                !status.is_navigation_sdk_notify_working ||
                !status.is_in_navigation || !status.is_on_road ||
                status.is_deviation)
            {
                return;
            }

            for (const auto& link : status.ahead_links)
            {
                if (link.link_match_status !=
                    NavigationStatus::LinkOnRoute::MatchStatus::Matched)
                {
                    continue;
                }
                for (const auto lane_group_id :
                     link.matched_lane_group_ids_on_route)
                {
                    std::cout << "Ahead LaneGroup " << lane_group_id << '\n';
                }
            }
        });
}

tn::hdmap::Status stopAheadRoadObservation(tn::hdmap::HDMapService& service)
{
    return service.setNavigationStatusListener(nullptr);
}

回调状态无效、车辆不在路、偏航或路线道路段未匹配时,不继续使用原有 Lane Group 结果。调用方检查 stopAheadRoadObservation(...) 的返回状态;停止成功或 shutdown() 完成后,再释放回调引用的上下文。回调线程与重入边界见路线关联与前路状态

查询 Lane Group 交通

本示例区分查询状态、空结果与可关联消息;完整的空结果、FreeFlow 和时效性语义见交通数据DataPending 可以在应用截止时间内有限重试。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void inspectLaneGroupTraffic(
    tn::hdmap::HDMapService& service,
    const std::vector<tn::hdmap::models::LaneGroupId>& lane_group_ids)
{
    std::vector<tn::hdmap::models::TrafficMessage> messages;
    const auto status = service.queryTraffic(messages, lane_group_ids);
    if (status == tn::hdmap::Status::DataPending)
    {
        std::cout << "Traffic data is still being prepared\n";
        return;
    }
    if (status == tn::hdmap::Status::DataNotFound)
    {
        std::cout << "No traffic data for the requested Lane Groups\n";
        return;
    }
    if (status != tn::hdmap::Status::OK)
    {
        std::cout << "Traffic query status=" << static_cast<int>(status) << '\n';
        return;
    }
    if (messages.empty())
    {
        std::cout << "No associated traffic message; freshness is unknown\n";
        return;
    }

    for (const auto& message : messages)
    {
        std::cout << "LaneGroup " << message.lane_group_id
                  << ", incidents=" << message.incidents.size()
                  << ", flow=" << static_cast<int>(message.flow.level)
                  << ", average speed(m/s)=" << message.flow.average_speed
                  << '\n';
    }
}

对象字段与交通数据语义见导航关联数据,状态与恢复动作见错误处理与可观测性