gpt4 book ai didi

c# - System.Device.Location.GeoCoordinateWatcher.Position.Location.Speed 始终为 NaN

转载 作者:行者123 更新时间:2023-11-30 23:29:21 28 4
gpt4 key购买 nike

我正在为我的 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";
}
}
}

最佳答案

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

关于c# - System.Device.Location.GeoCoordinateWatcher.Position.Location.Speed 始终为 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35516607/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com