在Windows Phone 8.1上获取CivicAddress

5

我正在尝试从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;

因为CivicAddress字段为空,所以会抛出NullReferenceException。我知道Windows 8没有提供CivicAddress提供程序。我想检查一下Windows Phone 8.1是否也是如此。如果是这样,我该如何获取/编写CivicAddress提供程序?

2个回答

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;

谢谢!我已经搜索了一段时间WinRT的解决方案。这个方法很有效。 - Scott Nimrod
1
这里不需要创建新的BasicGeoposition myLocation,可以直接使用position.Coordinate.Point作为参数传递给MapLocationFinder.FindLocationsAtAsync(); - vITs
@vITs 您是正确的,如果您想使用当前位置,您可以直接传递该位置。当您拥有经度和纬度(例如从数据库或其他地方)时,新的Geopoint将非常有用。 - Romasz
是的...如果你需要位置的其他属性,可以从数据库中获取坐标。 - vITs

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接