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

如何通过Azure Maps Search获取市政区域的边界多边形?

实现Azure Maps获取市政区域多边形边界

可以实现,你当前的代码未返回多边形是因为未启用几何数据返回配置,且参数设置不符合市政区域搜索场景,以下是具体解决方案:

方案一:修改现有SearchAddressAsync请求,启用几何数据返回

在请求配置中添加ReturnGeometry = true,同时移除针对点地址的ExtendedPostalCodesFor配置,修改后的代码如下:

var credential = new AzureKeyCredential("你的Azure Maps密钥");
var client = new MapsSearchClient(credential);
Response<SearchAddressResult> searchResult = await client.SearchAddressAsync(
    query: "Birmingham",
    options: new SearchAddressOptions
    {
        CountryFilter = new string[] { "GB" },
        Top = 1,
        EntityType = GeographicEntity.Municipality,
        ReturnGeometry = true // 关键配置:启用几何数据返回
    });

// 提取并遍历多边形边界点
if (searchResult.Value.Results?.FirstOrDefault()?.Geometry != null)
{
    var polygon = searchResult.Value.Results.First().Geometry.Polygon;
    // polygon为多层集合,外层是多边形环,内层是具体坐标点
    foreach (var ring in polygon)
    {
        foreach (var position in ring)
        {
            Console.WriteLine($"经度: {position.Longitude}, 纬度: {position.Latitude}");
        }
    }
}

方案二:使用SearchPolygonAsync接口(更适配行政区域多边形搜索)

该接口专门用于检索行政区域的多边形边界,代码示例:

var credential = new AzureKeyCredential("你的Azure Maps密钥");
var client = new MapsSearchClient(credential);
Response<SearchPolygonResult> polygonResult = await client.SearchPolygonAsync(
    query: "Birmingham",
    options: new SearchPolygonOptions
    {
        CountryFilter = new string[] { "GB" },
        EntityType = GeographicEntity.Municipality
    });

// 提取并处理边界数据
if (polygonResult.Value.Results?.FirstOrDefault()?.Geometry != null)
{
    var polygon = polygonResult.Value.Results.First().Geometry.Polygon;
    foreach (var ring in polygon)
    {
        foreach (var position in ring)
        {
            Console.WriteLine($"经度: {position.Longitude}, 纬度: {position.Latitude}");
        }
    }
}

注意事项

  • 确保你的Azure Maps资源已启用Search服务权限,且密钥拥有对应访问权限。
  • 部分行政区域可能包含多个多边形环(如含飞地),需遍历所有环以获取完整边界。
  • 若未返回多边形,可尝试调整EntityType(如GeographicEntity.Region)或扩大搜索范围,确认目标区域数据是否被Azure Maps收录。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 01:15:31