## didUpdateToLocation: calculate speed with distance and time

April 12, 2009 § 1 Comment

This is the easiest way to calculate distance, time and speed using newLocation and oldLocation using didUpdateToLocation: method.

– (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

//Calculate speed

CLLocationDistance distance;

NSTimeInterval timeElapsed;

//calculate distance and time elapsed only if we save old location

if(oldLocation != nil) {

distance = [newLocation getDistanceFrom:oldLocation];

timeElapsed = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];

}

else{

distance = 0.0;

timeElapsed = 1.0;

}

//Convert meter to km/h

Speed = ((distance*0.001)/timeElapsed)*3600;

NSLog(@”Speed: %f”, Speed);

//save the latest newLocation for calculating the speed

oldLocation = [newLocation retain];

}

Is this more accurate then the speed, distance and course properties provided by CLLocation?