0

In my iOS app I've to track the distance from a a start point to my current location. I implemented this code:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *currentLocation = [locations lastObject];
    CLLocation *startLocation = [locations firstObject];
    [coordArray addObject:currentLocation];
    float speed = currentLocation.speed * 3.6;
    if (speed > 0) {
        self.labelSpeed.text = [NSString stringWithFormat:@"%.2f Km/h", speed];
        [speedArray addObject:[NSNumber numberWithFloat:speed]];
    }
    CLLocationDistance distance = [startLocation distanceFromLocation:currentLocation];
}

But when I try to use the app it doesn't get a distance. I need the distance to show it in a label and with distance I will calculate the number of steps by using this equation:

steps = distance / length of human step

I know that it's not accurate, but I can't use accelerometer, because it doesn't work while the display of the iPhone is not active. A person suggested me this solution. Why my code doesn't give me a distance?

Community
  • 1
  • 1
lucgian841
  • 1,830
  • 5
  • 34
  • 57
  • 1
    What IS it doing. What IS the value of distance after you try to get the distance? What IS the value of currentLocation and startLocation? Unless you give us info about what it IS doing then we can't help much. "It isn't working" isn't very descriptive. – Fogmeister Mar 03 '14 at 10:07

2 Answers2

2

The callback

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

does give you at least one location, and locations array hold sonly multiple objects if the location update was deferred before. To get the walking/driving distance, you have to store the initial location in a class variable or property. Then, when you want to calculate a distance, do as in your code above, but with the class variable that holds the initial location.

Volker
  • 4,640
  • 1
  • 23
  • 31
  • Thank you, I understand my stupid mistake. So I've to store the start location in a property by this code `self.startLocation = [locations firstObject];` and after I should calculate the distance by this code `CLLocationDistance distance = [self.startLocation distanceFromLocation:currentLocation];` right? – lucgian841 Mar 03 '14 at 10:22
  • 2
    @lucgian841 : Except if you are looking for "how far from startPosition I'm" and not "how long I run", I'd like to add : you could not define distance between startPosition and currentPosition, you need to sum from pos1 to pos2 + from pos2 to pos3 + ... + from posx to currentPosition. otherwise, if the user run 50 km, then 49 km back, your distance will be 1km instead of 99km. Hop this help – Armand DOHM Mar 03 '14 at 10:25
0

Check below code for getting distance:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {


    if ([coordArray count]>0) {

        CLLocation *currentLocation = manager.location;
        CLLocation *startLocation = [coordArray objectAtIndex:0];
        [coordArray addObject:currentLocation];
        float speed = currentLocation.speed * 3.6;
        if (speed > 0) {
            NSLog(@"\n speed:%@",[NSString stringWithFormat:@"%.2f Km/h", speed]);
        }

        CLLocationDistance distance = [startLocation distanceFromLocation:currentLocation];

        if (distance>0) {
            NSLog(@"Distance:%@",[NSString stringWithFormat:@"%lf meters",distance]);
        }

    }
    else{
        [coordArray addObject:manager.location];
    }
}
jayraj m.g.
  • 625
  • 5
  • 18