Kia*_*eng 5 c# geolocation windows-runtime windows-phone-8 windows-phone-8.1
我正试图从Windows Phone 8.1中的Geoposition获取CivicAddress
我尝试使用以下代码:
// Get Current Location
var geolocator = new Geolocator();
geolocator.DesiredAccuracyInMeters = 100;
var position = await geolocator.GetGeopositionAsync();
// Get Country
var country = position.CivicAddress.Country;
Run Code Online (Sandbox Code Playgroud)
由于CivicAddress字段为null,因此抛出NullReferenceException.我知道没有为Windows 8提供CivicAddress提供程序.我想检查是否是Windows Phone 8.1的情况.如果是这样,我如何获取/编写CivicAddress提供程序?
Rom*_*asz 13
您需要使用ReverseGeocoding - 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;
Run Code Online (Sandbox Code Playgroud)