You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure Maps中集成Azure Traffic Incident API?

Azure Traffic Incident API 集成到 Azure Maps 实操指南

1. 前置准备

  • 确保你已拥有Azure Maps资源,并在Azure门户中获取到Maps订阅密钥(主密钥或次密钥均可)。
  • 熟悉Traffic Incident API的请求参数(如边界范围、事件类型过滤)和返回数据结构。

2. 调用API获取交通事件数据

通过HTTP GET请求获取指定区域的交通事件数据,示例请求格式如下:

GET https://atlas.microsoft.com/traffic/incidents/json?api-version=1.0&subscription-key={你的Maps密钥}&boundingBox={南,西,北,东}
  • boundingBox:指定查询的地理范围,格式为南纬度,西经度,北纬度,东经度,比如39.7,-105.0,40.0,-104.7。
  • 返回的JSON数据中,incidents数组包含所有事件,每个事件包含id、type、description、point(经纬度)等核心字段。

3. 在Azure Maps Web SDK中渲染事件

以下是前端集成的完整示例:

<!DOCTYPE html>
<html>
<head>
    <title>Azure Maps 交通事件展示</title>
    <meta charset="utf-8" />
    <script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.js"></script>
    <link rel="stylesheet" href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.css" />
    <style>
        #myMap { height: 100vh; width: 100%; }
    </style>
</head>
<body>
    <div id="myMap"></div>
    <script>
        // 初始化地图
        const map = new atlas.Map('myMap', {
            center: [-104.99, 39.74], // 初始中心点(以丹佛为例)
            zoom: 12,
            authOptions: {
                authType: 'subscriptionKey',
                subscriptionKey: '{你的Maps密钥}'
            }
        });

        // 地图加载完成后执行逻辑
        map.events.add('ready', async () => {
            // 获取当前地图视图的边界框
            const bounds = map.getCamera().bounds;
            const boundingBox = `${bounds.getSouth()},${bounds.getWest()},${bounds.getNorth()},${bounds.getEast()}`;

            // 调用Traffic Incident API
            const response = await fetch(`https://atlas.microsoft.com/traffic/incidents/json?api-version=1.0&subscription-key={你的Maps密钥}&boundingBox=${boundingBox}`);
            const data = await response.json();

            // 创建数据源并添加事件点
            const datasource = new atlas.source.DataSource();
            map.sources.add(datasource);

            // 将事件转换为GeoJSON点格式
            const incidentPoints = data.incidents.map(incident => {
                return new atlas.data.Feature(
                    new atlas.data.Point([incident.point.longitude, incident.point.latitude]),
                    {
                        id: incident.id,
                        type: incident.type,
                        description: incident.description,
                        severity: incident.severity
                    }
                );
            });
            datasource.add(incidentPoints);

            // 创建符号层渲染事件图标
            const symbolLayer = new atlas.layer.SymbolLayer(datasource, null, {
                iconOptions: {
                    image: 'accident', // 内置图标可选:'construction'、'traffic'等
                    size: 0.8
                }
            });
            map.layers.add(symbolLayer);

            // 添加点击弹窗交互
            const popup = new atlas.Popup();
            map.events.add('click', symbolLayer, (e) => {
                const properties = e.shapes[0].getProperties();
                popup.setOptions({
                    position: e.position,
                    content: `
                        <h3>事件ID: ${properties.id}</h3>
                        <p>类型: ${properties.type}</p>
                        <p>描述: ${properties.description}</p>
                        <p>严重程度: ${properties.severity}</p>
                    `
                });
                popup.open(map);
            });
        });
    </script>
</body>
</html>

4. 自定义优化建议

  • 分类渲染:根据事件类型(事故、施工、拥堵)配置不同图标,可替换iconOptions.image为自定义图标或内置图标。
  • 视图联动:监听地图moveend事件,在视野变化时重新调用API获取对应区域的事件数据。
  • 性能优化:对API返回数据做本地缓存,避免短时间内重复请求相同区域。
  • 安全升级:生产环境建议使用Azure AD认证替代密钥,提升权限控制安全性。

5. 常见问题排查

  • 返回数据为空:检查boundingBox范围是否合理,或该区域当前无交通事件。
  • API调用报错:核对订阅密钥是否正确,以及请求参数格式是否符合要求。

内容的提问来源于stack exchange,提问作者Teejaay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 16:01:10