2016-02-19 50 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,我会尝试做同样的事情。如果没有,你可能需要它来获得你正在寻找的东西。