在Windows Phone 8中获取思域地址

时间:2014-05-03 16:46:38

标签: windows-phone-8 geolocation

我正试图通过WP8中的Geolocator获取公民地址。但它会抛出 System.NullReferenceException

private  void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
     Dispatcher.BeginInvoke(() =>
      {
           LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString();
           LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString();
           Tblock.Text = args.Position.CivicAddress.Country;
      });
}

已经尝试了 Geoposition 。仍然抛出异常。试过有条件的检查,没用。请帮忙

[UPDATE]

点击按钮:

private void TrackLocation_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
     Geolocator geolocator = new Geolocator();
     geolocator.DesiredAccuracy = PositionAccuracy.High;
     geolocator.MovementThreshold = 1; // This units are in Meters
     geolocator.StatusChanged += geolocator_StatusChanged;
     geolocator.PositionChanged += geolocator_PositionChanged;
     TrackLocation.Content = "Stop Tracking";
}

2 个答案:

答案 0 :(得分:1)

如果你想获得一个职位的地址,那么我建议你使用ReverseGeocodeQuery API和你在Geolocator API中获得的位置,参考实现我在github有一个例子

答案 1 :(得分:0)

        var reportstatus = CivicFactory.Status;

        if (reportstatus == 4)
        {     
            var report = CivicFactory.CivicAddressReport;

            // Display the properties.
            tx1.value = report.AddressLine1;
            tx2.value = report.AddressLine2;
            tx3.value = report.City;
            tx4.value = report.StateProvince;
            tx5.value = report.CountryRegion;
            tx6.value = report.PostalCode;
            tx7.value = GetTimeString(report.Timestamp); 
        }
        else
        {
            alert("No location data provider available.");
        }