0

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);
DJ Burb
  • 2,346
  • 2
  • 29
  • 38
DaliGR
  • 9
  • 1

1 Answers1

0

How many iterations did you observe? Do you actually move hard when doing this experiment? GPS in the mode you are using might be really inaccurate, especially in closed spaces. Then, the new location might always come up as the same location and you would ended up adding zeros.

Terminus
  • 925
  • 10
  • 23
  • I take a number of iterations (more than 10). Point Distance (as calculated from the CLLocationDistance method varies between 3 and 10 (meters). The experiment is conducted in house but the results are the same when I use the app in the car... I am totally at a loss, maybe there is a bug in XCODE? At CLLocationDistance call the _poinDistance variable color is black (non ivar) and when I print it via NSLog the variable coror is shown as green (correct for ivar variable). – DaliGR Oct 08 '14 at 18:49