How to calculate speed using Core Location?

I need to get the speed of my device (meters per second), and this is my code, the speed is always 0, I do not 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];

thank

+3
source share
2 answers

I have never tried it before, but it is probably better if you set the DistanceFilter parameter in the location manager to 1 meter and calculate the time. then calculate the speed.

+1
source

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

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];
-1
source

Source: https://habr.com/ru/post/1756140/


All Articles