I have implemented the code below to calculate total distance in gps use by adding (accumulating) all point-to-point distances using the CLLocationDistance method. I create a counter (_locManagerIteration) and in the 1st iteration I copy the first locationManager object to my pastCoordinate property. On each next iteration, I use the CLLocationDistance method to calculate point-to-point distance between the pastCoordinate object and the new loc object. On program run, I see that the _pointDistance variable (double) is collecting the distance but the total distance (_totalDistance) variable (also double) is ALWAYS the same as the _pointDistance variable (i.e. no distance accumulating). For some inexplicable reason, the statement _totalDistance = _totalDistance + _pointDistance is either NOT executed, or the _totalDistance variable is always zero. Both NSLog statements below show the SAME result... Can you please help???
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
_locManagerIteration++;
CLLocation* loc = [locations lastObject];
if (_locManagerIteration == 1) {
self.pastCoordinate = [locations lastObject];
} else {
CLLocationDistance _pointDistance = [self.pastCoordinate distanceFromLocation:loc];
NSLog(@"POINT TO POINT DISTANCE = %f", _pointDistance);
_totalDistance = _totalDistance + _pointDistance;
self.pastCoordinate = [locations lastObject];
}
NSLog(@"TOTAL DISTANCE = %f", _totalDistance);