0

我有以下代码可以正常工作,只是它提供了重新启动应用程序时跟踪的最后位置详细信息。

在我单击 btnGetLocationDetails 按钮 2 或 3 次后,它会提供正确的位置详细信息。知道如何解决此问题,以便每次启动应用程序时,用户都无需单击此按钮 2 或 3 次即可获得正确的当前位置?

代码:

public partial class MainPage : PhoneApplicationPage
{

    GeoCoordinateWatcher watcher = null; 
    Location location=null;

    private void btnGetLocationDetails_Click_1(object sender, RoutedEventArgs e)
    {
        watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
        if (watcher.Permission == GeoPositionPermission.Granted)
        {
            watcher.MovementThreshold = Convert.ToDouble("100");
        }
        watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>(watcher_PositionChanged);

        //   PositionChanged events occur whenever your position changes     
        watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_OnStatusChanged);
        watcher.Start();
   }

   void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
   {
        //get the coordinates
        location = new Location();
        location.Latitude = e.Position.Location.Latitude;
        location.Longitude = e.Position.Location.Longitude;
        location.Altitude = e.Position.Location.Altitude;
   }


   void watcher_OnStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
   {            
        if (e.Status == GeoPositionStatus.Disabled)
           MessageBox.Show("The location service is turned off.");
        else if (e.Status == GeoPositionStatus.NoData)
     MessageBox.Show("No location data is available. ");
   }       
 }
4

0 回答 0