我正在开发位置跟踪应用程序。应用程序开始显示当前位置,后来显示不正确的值,代码如下。它在地图上绘制一条线(动画线)来跟踪我们的位置,它显示了错误的路径。虽然设备在固定位置,但它显示错误的值。我错过了什么吗?
//code
public partial class Walk : PhoneApplicationPage
{
private GeoCoordinateWatcher _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
private MapPolyline _line;
private DispatcherTimer _timer = new DispatcherTimer();
private long _startTime;
public Walk()
{
InitializeComponent();
_line = new MapPolyline();
_line.StrokeColor = Colors.Red;
_line.StrokeThickness = 5;
Map.MapElements.Add(_line);
_watcher.Start();
_timer.Start();
_startTime = System.Environment.TickCount;
_watcher.PositionChanged += Watcher_PositionChanged;
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
TimeSpan runTime = TimeSpan.FromMilliseconds(System.Environment.TickCount - _startTime);
timeLabel.Text = runTime.ToString(@"hh\:mm\:ss");
}
private double _kilometres;
private long _previousPositionChangeTick;
private void Watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var coord = new GeoCoordinate(e.Position.Location.Latitude, e.Position.Location.Longitude);
if (_line.Path.Count > 0)
{
var previousPoint = _line.Path.Last();
var distance = coord.GetDistanceTo(previousPoint);
var millisPerKilometer = (1000.0 / distance) * (System.Environment.TickCount - _previousPositionChangeTick);
_kilometres += distance / 1000.0;
paceLabel.Text = TimeSpan.FromMilliseconds(millisPerKilometer).ToString(@"mm\:ss");
distanceLabel.Text = string.Format("{0:f2} km", _kilometres);
caloriesLabel.Text = string.Format("{0:f0}", _kilometres * 65);
PositionHandler handler = new PositionHandler();
var heading = handler.CalculateBearing(new Position(previousPoint), new Position(coord));
Map.SetView(coord, Map.ZoomLevel, heading, MapAnimationKind.Parabolic);
}
else
{
Map.Center = coord;
}
_line.Path.Add(coord);
_previousPositionChangeTick = System.Environment.TickCount;
}
} }
}