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];
}

§ One Response to didUpdateToLocation: calculate speed with distance and time

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading didUpdateToLocation: calculate speed with distance and time at Web Builders.

meta

%d bloggers like this: