5

我想從可以將GeoPosition的CivicAddress中的Windows Phone 8.1獲取CivicAddress在Windows Phone 8.1

我用下面的代碼嘗試:

// Get Current Location 
var geolocator = new Geolocator(); 
geolocator.DesiredAccuracyInMeters = 100; 
var position = await geolocator.GetGeopositionAsync(); 

// Get Country 
var country = position.CivicAddress.Country; 

會拋出NullReferenceException異常的CivicAddress字段爲空。我知道Windows 8沒有提供CivicAddress提供程序。我想檢查Windows Phone 8.1是否屬於這種情況如果是這樣,我如何獲取/寫入CivicAddress提供程序?

回答

14

你將需要使用反向地理編碼爲此 - 更多信息at MSDN

至於Windows運行時比如,你可以使用MapLocationFinder.FindLocationsAtAsync用於此目的:

var geolocator = new Geolocator(); 
geolocator.DesiredAccuracyInMeters = 100; 
Geoposition position = await geolocator.GetGeopositionAsync(); 

// reverse geocoding 
BasicGeoposition myLocation = new BasicGeoposition 
    { 
     Longitude = position.Coordinate.Longitude, 
     Latitude = position.Coordinate.Latitude 
    }; 
Geopoint pointToReverseGeocode = new Geopoint(myLocation); 

MapLocationFinderResult result = await MapLocationFinder.FindLocationsAtAsync(pointToReverseGeocode); 

// here also it should be checked if there result isn't null and what to do in such a case 
string country = result.Locations[0].Address.Country; 
+0

THANK YOU!我搜索了一段時間的WinRT解決方案。這只是工作。 –

+0

@ScottNimrod歡迎您。我很高興它有幫助。 – Romasz

+1

這裏不需要創建新的BasicGeoposition myLocation,可以直接使用position.Coordinate.Point作爲參數來MapLocationFinder.FindLocationsAtAsync(); – vITs

相關問題