如何使用Azure Maps Web SDK访问Azure Maps数据注册表中的数据
使用Azure Maps Web SDK访问数据注册表中存储的Blob数据
目前Azure Maps Web SDK并未提供直接访问数据注册表的内置方法,但可以通过以下步骤实现:
实现步骤
通过数据注册表API获取Blob的授权SAS URL
调用数据注册表的SAS生成接口,获取目标文件的临时授权访问链接。请求格式如下:GET https://{region}.atlas.microsoft.com/dataRegistries/{udid}/sas?api-version=2023-06-01请求头需携带Azure Maps订阅密钥(或AAD令牌):
# 使用AAD令牌 Authorization: Bearer {AAD_TOKEN} # 或使用订阅密钥 Ocp-Apim-Subscription-Key: {YOUR_SUBSCRIPTION_KEY}接口返回的响应中会包含
sasUri字段,这是带权限的Blob访问链接。用Web SDK加载SAS URL指向的地理数据
拿到授权的SAS URL后,即可使用Web SDK的DataSource.importDataFromUrl方法加载数据,此时请求会被正常授权,无需担心CORS限制。
完整代码示例
// 初始化Azure Maps地图 const map = new atlas.Map('map', { center: [-122.33, 47.6], zoom: 12, authOptions: { authType: 'subscriptionKey', subscriptionKey: '{YOUR_SUBSCRIPTION_KEY}' } }); map.events.add('ready', async () => { const region = 'eu'; // 替换为你的Azure Maps区域 const udid = '{YOUR_DATA_REGISTRY_UDID}'; const apiVersion = '2023-06-01'; const subscriptionKey = '{YOUR_SUBSCRIPTION_KEY}'; // 1. 获取SAS URL const sasResponse = await fetch(`https://${region}.atlas.microsoft.com/dataRegistries/${udid}/sas?api-version=${apiVersion}`, { headers: { 'Ocp-Apim-Subscription-Key': subscriptionKey } }); const sasData = await sasResponse.json(); const sasUrl = sasData.sasUri; if (!sasUrl) { console.error('Failed to retrieve SAS URL'); return; } // 2. 加载数据到地图数据源 const datasource = new atlas.source.DataSource(); map.sources.add(datasource); await datasource.importDataFromUrl(sasUrl); // 添加图层渲染数据 map.layers.add(new atlas.layer.SymbolLayer(datasource)); });
注意事项
- 确保数据注册表已正确关联Blob存储,且文件已成功注册(你之前验证的列出、分析文件操作正常即可)
- SAS URL具有时效性,若需要长期访问,可在请求SAS接口时通过
durationInSeconds参数设置有效期(默认3600秒) - 若仍遇到CORS问题,需确保Blob存储的CORS规则允许你的网站域名发起请求
内容的提问来源于stack exchange,提问作者38fuenf
相关产品推荐
相关产品推荐

