執行地理編碼和反向地理編碼
重要
Bing 地圖服務企業版服務淘汰
來自 Windows.Services.Maps 命名空間的 UWP MapControl 和地圖服務依賴 Bing 地圖服務。 Bing Enterprise 地圖服務已被取代,且將會淘汰,此時 MapControl 和服務將不再接收數據。
如需詳細資訊,請參閱 Bing 地圖服務開發人員中心和Bing 地圖服務檔。
注意
MapControl 和地圖服務要求地圖驗證金鑰,稱為 MapServiceToken。 如需取得和設定地圖驗證金鑰的詳細資訊,請參閱要求地圖驗證金鑰。
本指南展示了如何透過呼叫 Windows.Services.Maps 命名空間中的 MapLocationFinder 類別的方法,將街道地址轉換為地理位置 (地理編碼) 以及將地理位置轉換為街道地址 (反向地理編碼)。
地理編碼和反向地理編碼所涉及的類別會依下列方式組織。
- MapLocationFinder 類別包含處理地理編碼 (FindLocationsAsync) 和反向地理編碼 (FindLocationsAtAsync) 的方法。
- 這些方法都會傳回 MapLocationFinderResult 執行個體。
- MapLocationFinderResult 的 Locations 屬性公開了 MapLocation 物件的集合。
- MapLocation 物件具有 Address 屬性和 Point 屬性。Address 屬性公開代表街道地址的 MapAddress 物件,而 Point 屬性則公開代表地理位置的 Geopoint 物件。
重要
您必須先指定地圖驗證金鑰,才能使用地圖服務。 如需詳細資訊,請參閱要求地圖驗證金鑰。
取得位置 (地理編碼)
本節說明如何將街道位址或地名轉換為地理位置 (地理編碼)。
- 使用 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 方法會傳回 MapLocationFinderResult 物件,其中包含相符 MapLocation 物件的集合。
- 使用 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
相關主題
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應