本文內容
注意
和地圖服務需要稱為 的地圖身份驗證密鑰。 有關獲取和設置地圖身份驗證密鑰的詳細信息ios 地理位置反編碼,請參閱請求地圖身份驗證密鑰。
本指南介紹如何通過調用 ..Maps 命名空間中 類的方法將街道地址轉換為地理位置(地理編碼)以及將地理位置轉換為街道地址(反向地理編碼)。
提示
若要詳細了解如何在應用中使用地圖,請從 上的 通用示例存儲庫下載 示例。
地理編碼和反向地理編碼中涉及的類按如下方式進行組織。
重要
必須先指定地圖驗證密鑰,才能使用地圖服務。 有關詳細信息,請參閱請求地圖身份驗證密鑰。
獲取位置(地理編碼)
本部分說明如何將街道地址或地點名稱轉換為地理位置(地理編碼)。
使用地點名稱或街道地址調用 類的 方法的其中一項重載。 方法返回一個 ult 對象。使用 ult 的 屬性公開 對象的集合。 可能有多個 對象,因為系統可能會找到與給定輸入對應的多個位置。
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() + ")";
}
}
此代碼向 文本框顯示以下結果。
result = (47.6406099647284,-122.129339994863)
獲取地址(反向地理編碼)
本部分介紹如何將地理位置轉換為地址(反向地理編碼)。
調用 類的 方法。 方法將返回一個 ult 對象,該對象包含匹配的 對象的集合。使用 ult 的 屬性公開 對象的集合。 可能有多個 對象ios 地理位置反編碼,因為系統可能會找到與給定輸入對應的多個位置。通過每個 的 屬性來訪問 對象。
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;
}
}
此代碼向 文本框顯示以下結果。
town = Redmond
相關主題