System.Device.Location.GeoCoordinateWatcher.Position.Location.Speed 始终为 NaN

Posted

技术标签:

【中文标题】System.Device.Location.GeoCoordinateWatcher.Position.Location.Speed 始终为 NaN【英文标题】:System.Device.Location.GeoCoordinateWatcher.Position.Location.Speed always is NaN 【发布时间】:2016-02-19 22:58:24 【问题描述】:

我正在为我的 WP8.1 设备制作一个简单的应用程序,它将跟踪我的最大速度。我为此使用 System.Device.Location.GeoCoordinateWatcher。我可以检测到我的位置,但速度始终是 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";
        
    

【问题讨论】:

【参考方案1】:

我认为您的代码没有问题。我在安装了 WiFi、蜂窝和 GPS 的设备上执行类似的功能。看来蜂窝是最快的锁定并且不提供任何速度数据。但是,当我禁用手机和 WiFi 时,我可以从 GPS 传感器获取速度数据。如果您有专用的 GPS,我会尝试做同样的事情。如果没有,您可能需要它来获得所需的内容。

【讨论】:

以上是关于System.Device.Location.GeoCoordinateWatcher.Position.Location.Speed 始终为 NaN的主要内容,如果未能解决你的问题,请参考以下文章