Reputation: 810
I am working on location tracking app. App is showing the current location in the beginning and later it is displaying incorrect values and the code is as follows. It draws a line(animated line) on the Map tracking our location and it is showing wrong path. Though device is at constant place, it is displaying wrong values. Did I miss anything?
//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;
}
} }
}
Upvotes: 0
Views: 833
Reputation: 206
Are the "incorrect values" within a few dozen meters of the original location? If so, what you might be seeing is just the inaccuracy of GPS.
GPS isn't 100% accurate. Even if your device isn't moving, the location where the device's GPS thinks it is can change. In fact, your calculated location might not be based on true GPS at all, but on Wi-Fi or on nearby cell towers (A-GPS), both of which are much less accurate.
See this StackOverflow thread for more information. For debugging purposes, I suggest displaying the HorizontalAccuracy somewhere on your UI to see if the GPS drift is in line with expectations.
Upvotes: 2