我正在计算距离和速度,如下所示。
totalDistance=totalDistance+[newLocation distanceFromLocation:self
->tempOldLocation];
distanceLabel.text= [NSString stringWithFormat:@"%.2f km", (totalDistance
/1000)];
self->tempOldLocation=newLocation;
计算速度:
CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
NSTimeInterval sinceLastUpdate = [newLocation.timestamp
timeIntervalSinceDate:oldLocation.timestamp];
double calculatedSpeed = distanceChange / sinceLastUpdate;
我想以km / h计算速度。请建议我以km / h计算速度的任何方法。
答案 0 :(得分:1)
您将直接从代理方法CLLocationSpeed
获得的CLLocation
didUpdateLocation
属性获得速度。
答案 1 :(得分:0)
看起来你将米分为秒,给你每秒米。 将米转换为KMS,将秒转换为小时
double calculatedSpeed = (distanceChange / 1000) / (sinceLastUpdate / 60 / 60)
答案 2 :(得分:0)
在CLLocationManagerDelegate中使用方法didUpdateLocations:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let speed = locations.last?.speed else { return }
viewModel.speed.value = speed * 3600/1000
}