我为我的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";
}
}
}
}
答案 0 :(得分:2)
我不相信您的代码存在问题。我在安装了WiFi,蜂窝和GPS的设备上做了类似的功能。似乎蜂窝电话是最快锁定的,并且不提供任何速度数据。但是,当我禁用小区和WiFi时,我从GPS传感器获得速度数据就好了。如果你有专用的GPS,我会尝试做同样的事情。如果没有,您可能需要它来获得您想要的东西。