从Azure Maps搜索获取多边形

huangapple go评论107阅读模式
英文:

Get polygon from Azure Maps Search

问题

我正在尝试使用Azure.Maps.Search来为我提供结果的多边形。例如,如果我搜索“Birmingham”,我希望得到该市的边界定义的一组地理点的结果。

这是可能的吗?

英文:

I'm trying to use Azure.Maps.Search to give me a polygon for a result. For example, if I search for "Birmingham" I would like a result for that municipality with a collection of geopoints defining the boundary.

Is this possible?

			var credential = new AzureKeyCredential("............");
			var client = new MapsSearchClient(credential);
			Response<SearchAddressResult> searchResult = await client.SearchAddressAsync(
				query: "Birmingham",
				options: new SearchAddressOptions
				{
					ExtendedPostalCodesFor=new SearchIndex[] { SearchIndex.PointAddresses },
					CountryFilter = new string[] { "GB" },
					Top = 1,
					EntityType = GeographicEntity.Municipality
				});

从Azure Maps搜索获取多边形

答案1

得分: 1

是的,这是可能的。搜索地址API的结果中将包含一个DataSources.Geometries.ID值,该值是该结果的唯一边界的ID。您可以取得这个ID,并将其传递给Azure.Maps.Search Nuget package中的GetPolygonsAsync API。

using Azure;
using Azure.Maps.Search;
using Azure.Maps.Search.Models;

namespace AzureMapsTest
{
	internal class Program
	{
		private const string MapsApiKey = "...........";

		static async Task Main(string[] args)
		{
			var credential = new AzureKeyCredential(MapsApiKey);
			var client = new MapsSearchClient(credential);
			SearchAddressOptions singleSearchResultOptions = new SearchAddressOptions { Top = 1 };
			Response<SearchAddressResult> searchResult =
				await client.SearchAddressAsync(
					"Ealing, London, England",
					singleSearchResultOptions);

			Response<PolygonResult> polygons =
				await client.GetPolygonsAsync(new string[] { searchResult.Value.Results[0].DataSources.Geometry.Id });

			Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(polygons.Value.Polygons));
		}
	}
}
英文:

Yes, this is possible. The search address API will contain a DataSources.Geometries.ID value in the results that is the ID of the unique boundary for that result. You can take this ID and pass it into the GetPolygonsAsync API in the Azure.Maps.Search Nuget package.

using Azure;
using Azure.Maps.Search;
using Azure.Maps.Search.Models;

namespace AzureMapsTest
{
	internal class Program
	{
		private const string MapsApiKey = &quot;...........&quot;;

		static async Task Main(string[] args)
		{
			var credential = new AzureKeyCredential(MapsApiKey);
			var client = new MapsSearchClient(credential);
			SearchAddressOptions singleSearchResultOptions = new SearchAddressOptions { Top = 1 };
			Response&lt;SearchAddressResult&gt; searchResult =
				await client.SearchAddressAsync(
					&quot;Ealing, London, England&quot;,
					singleSearchResultOptions);

			Response&lt;PolygonResult&gt; polygons =
				await client.GetPolygonsAsync(new string[] { searchResult.Value.Results[0].DataSources.Geometry.Id });

			Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(polygons.Value.Polygons));
		}
	}
}

huangapple
  • 本文由 发表于 2023年2月8日 20:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定