views:

33

answers:

2

hi , i need to get the speed of my device (meters per seconds) and this is my code the speed is always 0 i don't understand.

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

        double gpsSpeed2 = newLocation.speed;

        labelm.text = [NSString stringWithFormat:@"%f",gpsSpeed2];
    }



locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

thanks

A: 

You need to do the calculation yourself. Something like this:

if (oldLocation != nil)
{
   CLLocationDistance distance = [newLocation getDistanceFrom:oldLocation];
   NSTimeInterval timeElapsed = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];

   // Use distance and timeElapsed to calculate speed
}
else {
   // We don't have and old time, so can't calculate speed
}    

oldLocation = [newLocation retain];
Martin Ingvar Kofoed Jensen
A: 

I never tried it before but probably its better if you set the distanceFilter in the location manager to a number like 1 meter and calculate the time. then calculate the speed.

YNK