3

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

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
hugo411
  • 320
  • 1
  • 12
  • 29

2 Answers2

1

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
  • 869
  • 6
  • 12
-1

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