Search code examples
iosobjective-cperformanceios7core-location

how to calculate speed in ios 7 using coreLocation framework


prior to iOS 7 i used to calculate speed as below

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

      double speed = newLocation.speed;
      NSLog(@"Speed of Device is %f",newLocation.speed); 

      // manual method
      if(oldLocation != nil)
      {
           CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
           NSTimeInterval sinceLastUpdate = [newLocation.timestamp 
           timeIntervalSinceDate:oldLocation.timestamp];
           double calculatedSpeed = distanceChange / sinceLastUpdate;

           NSLog(@"Speed of Device is %f",calculatedSpeed); 
     }  
 }  

since this method is deprecated ,. please suggest me another way to calculate speed using iOS7 using CoreLocation.


Solution

  • You can do the following:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        CLLocation *loc = locations.lastObject;
        double speed = loc.speed;
        NSLog(@"%f", speed);
    }