ジオコーディングと逆ジオコーディングの実行
注意
MapControl とマップ サービスには、MapServiceToken と呼ばれるマップ認証キーが必要です。 マップ認証キーを取得して設定する方法について詳しくは、「マップ認証キーの要求」をご覧ください。
このガイドでは、Windows.Services.Maps 名前空間の MapLocationFinder クラスのメソッドを呼び出して、住所から地理的な位置への変換 (ジオコーディング) や地理的な位置から住所への変換 (逆ジオコーディング) を行う方法について説明します。
ヒント
アプリで地図を使用する方法の詳細については、GitHub の Windows universal samples リポジトリから MapControl サンプルをダウンロードしてください。
ジオコーディングと逆ジオコーディングに関連するクラスは、次のように編成されています。
- MapLocationFinder クラスには、ジオコーディング (FindLocationsAsync) と逆ジオコーディング (FindLocationsAtAsync) を処理するメソッドが含まれています。
- これらのメソッドは、どちらも MapLocationFinderResult インスタンスを返します。
- MapLocationFinderResult の Locations プロパティは、MapLocation オブジェクトのコレクションを公開します。
- MapLocation オブジェクトには、住所を表している MapAddress オブジェクトを公開する Address プロパティと、地理的な位置を表している Geopoint オブジェクトを公開する Point プロパティの両方があります。
重要
マップ サービスを使用する前に、マップ認証キーを指定する必要があります。 詳しくは、「マップ認証キーの要求」をご覧ください。
位置情報の取得 (ジオコーディング)
このセクションでは、住所または地名を地理的な位置 (ジオコーディング) に変換する方法について説明します。
- MapLocationFinder クラスの FindLocationsAsync メソッドのいずれかのオーバーロードを、地名または住所を指定して呼び出します。
- FindLocationsAsync メソッドは、MapLocationFinderResult オブジェクトを返します。
- MapLocationFinderResult の Locations プロパティを使用して、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)
住所の取得 (逆ジオコーディング)
このセクションでは、地理的な位置を住所に変換する (逆ジオコーディング) 方法について説明します。
- MapLocationFinder クラスの FindLocationsAtAsync メソッドを呼び出します。
- FindLocationsAtAsync メソッドは、一致する MapLocation オブジェクトのコレクションを含む MapLocationFinderResult オブジェクトを返します。
- MapLocationFinderResult の Locations プロパティを使用して、MapLocation オブジェクトのコレクションを公開します。 指定された入力に対応する複数の場所をシステムが見つける可能性があるため、複数の MapLocation オブジェクトがある場合があります。
- 各 MapLocation の Address プロパティを通じて 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
関連トピック
- UWP の地図のサンプル
- UWP の交通情報アプリのサンプル
- 地図の設計ガイドライン
- ビデオ: Windows アプリでの電話、タブレット、PC で使用できるマップと位置情報の活用
- Bing Maps Developer Center
- MapLocationFinder クラス
- FindLocationsAsync メソッド
- FindLocationsAtAsync メソッド
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示