Windows phone 8 Geolocator Exception

时间:2015-07-16 18:28:18

标签: c# wpf windows-phone-8

嗨,我是WP开发的新手。我想找到用户当前的邮政编码或邮政编码。所以我有以下代码。问题是这一行d.zip = position.CivicAddress.PostalCode;总是给对象引用没有设置为对象异常的实例。但我能够得到纬度和经度就好了。我也尝试过position.CivicAddresss.City但仍然是同样的异常。请帮忙。

  async void Button_Click_1(object sender, RoutedEventArgs e)
           {
               Data d = new Data();
               Geolocator locator = new Geolocator();
               try
               {
                   Geoposition position = await locator.GetGeopositionAsync();
                   d.zip = position.CivicAddress.PostalCode;
                   d.Latitude = position.Coordinate.Latitude;
                   d.Longitude = position.Coordinate.Longitude;
                   MessageBox.Show(d.zip);
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
           }

    public class Data
    {
        public string zip { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

我相信不推荐使用GeoPosition.CivicAddress。您需要使用MapLocationFinder来获取地址信息。

async void Button_Click_1(object sender, RoutedEventArgs e)
{
    Data d = new Data();
    Geolocator locator = new Geolocator();
    try
    {
        Geoposition position = await locator.GetGeopositionAsync();

        // Get address data with MapLocationFinder
        var result = await MapLocationFinder.FindLocationsAtAsync(position.Coordinate.Point);
        if (result.Status == MapLocationFinderStatus.Success)
        {
            var address = result.Locations[0].Address;
            var zip = address.PostCode;
            d.zip = zip;
        }

        // These are deprecated as well. 
        // Use position.Coordinate.Point.Position.Latitude and
        // position.Coordinate.Point.Position.Longitude

        d.Latitude = position.Coordinate.Latitude;
        d.Longitude = position.Coordinate.Longitude;
        MessageBox.Show(d.zip);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

public class Data
{
    public string zip { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}