ジオコーディングと逆ジオコーディングの実行

注意

MapControl とマップ サービスには、MapServiceToken と呼ばれるマップ認証キーが必要です。 マップ認証キーを取得して設定する方法について詳しくは、「マップ認証キーの要求」をご覧ください。

このガイドでは、Windows.Services.Maps 名前空間の MapLocationFinder クラスのメソッドを呼び出して、住所から地理的な位置への変換 (ジオコーディング) や地理的な位置から住所への変換 (逆ジオコーディング) を行う方法について説明します。

ヒント

アプリで地図を使用する方法の詳細については、GitHub の Windows universal samples リポジトリから 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 メソッドは、一致する 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