GeoCoordinateWatcher location not returning speed. NaN instead

我怕爱的太早我们不能终老 提交于 2019-12-11 07:55:53

问题


I've been experimenting with getting readings from the GPS with my wp7.

I'm using very similar code to the sample found here: Getting GPS coordinates on Windows phone 7

I've got 2 textblocks on the page. One showing the speed from Position.Location.Speed and one showing the timestamp from Position.Location.TimeStamp. This is triggered from the watcher_PositionChanged event.

I notice the event being triggered every second or so, as the timestamp updates to reflect this. I'm also reading the GPS status as "ready" (the first couple of readings are "No Data") However, the speed value continues to display NaN.

I loaded this code onto a physical device (LG Optimus 7), and drove down the street with the phone on the dashboard to test.


回答1:


OK, I realized that GeoCoordinateWatcher runs on the UI thread and it was acting a bit.. strange. Moving this off into a separate thread fixed the problem.




回答2:


Try to do it with that way:

void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    // you cannot change the UI in this function -> you have to call the UI Thread
    Deployment.Current.Dispatcher.BeginInvoke(() => ChangeUI(e));
}
void ChangeUI(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    // do magic 
}


来源:https://stackoverflow.com/questions/5175079/geocoordinatewatcher-location-not-returning-speed-nan-instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!