Vineet Singh
Vineet Singh

Reputation: 4019

CLLocationManager - Calculate real time speed on iPhone

How can I calculate real time speed on device? I've googled a lot but all I got is to calculate distance and speed after completion of journey. Can I calculate speed at runtime?

Suggestions will be much appreciated.

Thanks in advance.

Upvotes: 5

Views: 7676

Answers (2)

Paras Joshi
Paras Joshi

Reputation: 20541

here CLLocationManager class provide different field of location like, Latitude, Longitude,accuracy and speed.

i use CoreLocationController so for location update i call this bellow method you can get current speed in - (void)locationUpdate:(CLLocation *)location method like bellow

  - (void)locationUpdate:(CLLocation *)location
{
     NSString *currentspeed = [NSString stringWithFormat:@"SPEED: %f", [location speed]];
}

or otherwise bellow is delegate method

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"in update to location");
    NSString *currentspeed = [NSString stringWithFormat:@"SPEED: %f", [newLocation speed]];
}

also you can get example from this link... http://www.vellios.com/2010/08/16/core-location-gps-tutorial/

i hope this help you... :)

Upvotes: 2

Adeel Pervaiz
Adeel Pervaiz

Reputation: 1356

There is a delegate CLLocationManagerDelegate add it to your controller header file.

Initialize Location Manager and set the delegate where you implement the delegate method for location Manager like this

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; // send loc updates to current class

and you can write method in your class

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"Location Speed: %f", newLocation.speed);
}

That's it you will get your location updates in the above method with the speed also, and it will trigger as frequent as possible.

Upvotes: 0

Related Questions