4

I am calculating the distance and speed as below.

totalDistance=totalDistance+[newLocation distanceFromLocation:self
->tempOldLocation];
distanceLabel.text= [NSString stringWithFormat:@"%.2f km", (totalDistance
/1000)];
self->tempOldLocation=newLocation;

Calculating Speed:

CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
NSTimeInterval sinceLastUpdate = [newLocation.timestamp 
       timeIntervalSinceDate:oldLocation.timestamp];
double calculatedSpeed = distanceChange / sinceLastUpdate;

I want to calculate the speed in km/h. Please suggest me any way to calculate the speed in km/h.

user3616535
  • 119
  • 1
  • 9
  • try this link http://stackoverflow.com/questions/5988590/how-to-calculate-the-current-speed-and-average-speed-of-user-travelling-from-cur – Anbu.Karthik Feb 03 '15 at 07:07

3 Answers3

1

It looks like you are dividing meters to seconds, giving you meters per second. Convert meters to KMS, and seconds to hours

double calculatedSpeed = (distanceChange / 1000) / (sinceLastUpdate / 60 / 60)
Schwarz Software
  • 1,114
  • 8
  • 22
1

You will get speed directly from the CLLocationSpeed property of CLLocation obtained at delegate method didUpdateLocation.

krishnanunni
  • 510
  • 7
  • 16
0

In CLLocationManagerDelegate use method didUpdateLocations:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let speed = locations.last?.speed else { return }

    viewModel.speed.value = speed * 3600/1000
}
Evgeny Karev
  • 605
  • 6
  • 13