지오코딩 및 역방향 지오코딩 수행

참고 항목

MapControl 및 지도 서비스를 사용하려면 MapServiceToken이라는 지도 인증 키가 필요합니다. 맵 인증 키 가져오기 및 설정에 대한 자세한 내용은 맵 인증 키 요청을 참조하세요.

이 가이드에서는 Windows.Services.Maps 네임스페이스에 있는 MapLocationFinder 클래스의 메서드를 호출하여 주소를 지리적 위치로 변환하고(지오코딩) 지리적 위치를 주소로 변환(리버스 지오코딩)하는 방법을 보여 줍니다.

앱에서 지도를 사용하는 방법을 알아보려면 GitHub의 Windows 유니버설 샘플 리포지토리에서 MapControl 샘플을 다운로드하세요.

지오코딩 및 리버스 지오코딩과 관련된 클래스는 다음과 같이 구성됩니다.

Important

 지도 서비스를 사용하려면 먼저 지도 인증 키를 지정해야 합니다. 자세한 내용은 지도 인증 키 요청을 참조하세요.

위치(지오코드) 가져오기

이 섹션에서는 주소 또는 장소 이름을 지리적 위치로 변환(지오코딩)하는 방법을 보여 줍니다.

  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 메서드는 일치하는 MapLocation 개체의 컬렉션을 포함하는 MapLocationFinderResult 개체를 반환합니다.
  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