开发者

GeoCoordinateWatcher location not returning speed. NaN instead

开发者 https://www.devze.com 2023-02-14 06:45 出处:网络
I\'ve been experimenting with getting readings from the GPS with my wp7. I\'m using very similar code to the sample found here:

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 开发者_Go百科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.


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.


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 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消