2016-02-19 71 views
1

我正在爲我的WP8.1設備製作一個簡單的應用程序,它將跟蹤我的最高速度。我爲此使用了System.Device.Location.GeoCoordinateWatcher。我可以檢測到我的位置,但速度總是NaN。我不明白,爲什麼。哪裏不對?感謝您的任何幫助或信息。這是我下面全碼:System.Device.Location.GeoCoordinateWatcher.Position.Location.Speed永遠是NaN

namespace SpeedTracker 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     GeoCoordinateWatcher watcher; 
     double maxSpeed = 0.0; 

    public MainPage() 
    { 
     InitializeComponent();    
    } 

    private void StartTrackingBtn_Click(object sender, RoutedEventArgs e) 
    { 
     this.watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default); 

     this.watcher.MovementThreshold = 10; 
     this.watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged); 
     this.watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged); 

     this.watcher.Start(); 
    } 

    private void StopTrackingBtn_Click(object sender, RoutedEventArgs e) 
    { 
     this.watcher.StatusChanged -= new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged); 
     this.watcher.PositionChanged -= new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged); 

     this.watcher.Stop(); 
    } 

    private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e) 
    { 
     if (this.watcher.Position.Location.IsUnknown != true) 
     { 
      Deployment.Current.Dispatcher.BeginInvoke(() => 
      { 
       this.maxSpeed = Math.Max(this.maxSpeed, e.Position.Location.Speed); 
       this.SpeedValueTxblck.Text = this.maxSpeed.ToString(); 
      }); 
     } 
     else 
     { 
      this.SpeedValueTxblck.Text = "Please wait while your prosition is determined..."; 
     } 
    } 

    private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e) 
    { 
     switch (e.Status) 
     { 
      case GeoPositionStatus.Disabled: 
       Deployment.Current.Dispatcher.BeginInvoke(() => 
       { 
        this.SpeedValueTxblck.Text = "Location Service is not enabled on the device"; 
       }); 
       break; 

      case GeoPositionStatus.NoData: 
       Deployment.Current.Dispatcher.BeginInvoke(() => 
       { 
        this.SpeedValueTxblck.Text = "The Location Service is working, but it cannot get location data"; 
       }); 
       break; 
      default: 
       break; 
     } 
    } 

    private void GetLocationCourseAndSpeed() 
    { 
     this.watcher.TryStart(true, TimeSpan.FromMilliseconds(1000)); 

     if (watcher.Position.Location.IsUnknown != true) 
     { 
      GeoCoordinate coord = watcher.Position.Location; 

      this.maxSpeed = Math.Max(this.maxSpeed, coord.Speed); 

      this.SpeedValueTxblck.Text = this.maxSpeed.ToString(); 
     } 
     else 
     { 
      this.SpeedValueTxblck.Text = "Unknown"; 
     } 
    } 
} 

}

回答

2

我不相信有你的代碼的問題。我正在對安裝了WiFi,Cellular和GPS的設備執行類似的功能。看起來蜂窩是最快速鎖定的,並且不會提供任何速度數據。但是,當我禁用單元和WiFi時,我從GPS傳感器獲得速度數據就好了。如果你有專用的GPS,我會嘗試做同樣的事情。如果沒有,你可能需要它來獲得你正在尋找的東西。