执行地理编码和反向地理编码

注意

MapControl 和地图服务需要称为 MapServiceToken 的地图身份验证密钥。 有关获取和设置地图身份验证密钥的详细信息,请参阅请求地图身份验证密钥

本指南介绍如何通过调用 Windows.Services.Maps 命名空间中 MapLocationFinder 类的方法将街道地址转换为地理位置(地理编码)以及将地理位置转换为街道地址(反向地理编码)。

提示

若要详细了解如何在应用中使用地图,请从 GitHub 上的 Windows 通用示例存储库下载 MapControl 示例。

地理编码和反向地理编码中涉及的类按如下方式进行组织。

重要

 必须先指定地图验证密钥,才能使用地图服务。 有关详细信息,请参阅请求地图身份验证密钥

获取位置(地理编码)

本部分说明如何将街道地址或地点名称转换为地理位置(地理编码)。

  1. 使用地点名称或街道地址调用 MapLocationFinder 类的 FindLocationsAsync 方法的其中一项重载。
  2. FindLocationsAsync 方法返回一个 MapLocationFinderResult 对象。
  3. 使用 MapLocationFinderResultLocations 属性公开 MapLocation 对象的集合。 可能有多个 MapLocation 对象,因为系统可能会找到与给定输入对应的多个位置。
using Windows.Services.Maps;
using Windows.Devices.Geolocation;
...
private async void geocodeButton_Click(object sender, RoutedEventArgs e)
{
   // The address or business to geocode.
   string addressToGeocode = "Microsoft";

   // The nearby location to use as a query hint.
   BasicGeoposition queryHint = new BasicGeoposition();
   queryHint.Latitude = 47.643;
   queryHint.Longitude = -122.131;
   Geopoint hintPoint = new Geopoint(queryHint);

   // Geocode the specified address, using the specified reference point
   // as a query hint. Return no more than 3 results.
   MapLocationFinderResult result =
         await MapLocationFinder.FindLocationsAsync(
                           addressToGeocode,
                           hintPoint,
                           3);

   // If the query returns results, display the coordinates
   // of the first result.
   if (result.Status == MapLocationFinderStatus.Success)
   {
      tbOutputText.Text = "result = (" +
            result.Locations[0].Point.Position.Latitude.ToString() + "," +
            result.Locations[0].Point.Position.Longitude.ToString() + ")";
   }
}

此代码向 tbOutputText 文本框显示以下结果。

result = (47.6406099647284,-122.129339994863)

获取地址(反向地理编码)

本部分介绍如何将地理位置转换为地址(反向地理编码)。

  1. 调用 MapLocationFinder 类的 FindLocationsAtAsync 方法。
  2. FindLocationsAtAsync 方法将返回一个 MapLocationFinderResult 对象,该对象包含匹配的 MapLocation 对象的集合。
  3. 使用 MapLocationFinderResultLocations 属性公开 MapLocation 对象的集合。 可能有多个 MapLocation 对象,因为系统可能会找到与给定输入对应的多个位置。
  4. 通过每个 MapLocationAddress 属性来访问 MapAddress 对象。
using Windows.Services.Maps;
using Windows.Devices.Geolocation;
...
private async void reverseGeocodeButton_Click(object sender, RoutedEventArgs e)
{
   // The location to reverse geocode.
   BasicGeoposition location = new BasicGeoposition();
   location.Latitude = 47.643;
   location.Longitude = -122.131;
   Geopoint pointToReverseGeocode = new Geopoint(location);

   // Reverse geocode the specified geographic location.
   MapLocationFinderResult result =
         await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode);

   // If the query returns results, display the name of the town
   // contained in the address of the first result.
   if (result.Status == MapLocationFinderStatus.Success)
   {
      tbOutputText.Text = "town = " +
            result.Locations[0].Address.Town;
   }
}

此代码向 tbOutputText 文本框显示以下结果。

town = Redmond