【问题标题】:Getting CivicAddress on Windows Phone 8.1在 Windows Phone 8.1 上获取 CivicAddress
【发布时间】:2014-07-04 17:17:00
【问题描述】:

我正在尝试从 Windows Phone 8.1 中的地理位置获取 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 提供程序?

【问题讨论】:

    标签: c# windows-phone-8 geolocation windows-runtime windows-phone-8.1


    【解决方案1】:

    您需要为此使用 ReverseGeocoding - 更多信息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;
    

    【讨论】:

    • 谢谢!我已经搜索了一段时间的 WinRT 解决方案。这行得通。
    • 这里不需要创建新的BasicGeoposition myLocation,可以直接使用position.Coordinate.Point作为MapLocationFinder.FindLocationsAtAsync()的参数;
    • @vITs 你是对的,如果你想使用当前位置,你可以直接传递位置。一旦有了经度和纬度(例如来自数据库或其他地方),新的 Geopoint 就会很有用。
    • 是的...如果您想要来自 DB 的 position.Coordinate 的其他属性。
    【解决方案2】:

    如果您想获取某个职位的地址,那么我建议您将 ReverseGeocodeQuery API 与您通过 Geolocator API 获得的职位一起使用,作为参考实现,我在 github 上确实有一个示例https://github.com/nokia-developer/maps-samples/tree/master/RevGeoCoding

    否则,您也可以尝试从 GeoCoordinates 获取城市地址 http://msdn.microsoft.com/en-us/library/system.device.location.civicaddress(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多