Ios 使用核心位置框架计算以公里/小时为单位的速度
我计算的距离和速度如下Ios 使用核心位置框架计算以公里/小时为单位的速度,ios,objective-c,core-location,Ios,Objective C,Core Location,我计算的距离和速度如下 totalDistance=totalDistance+[newLocation distanceFromLocation:self ->tempOldLocation]; distanceLabel.text= [NSString stringWithFormat:@"%.2f km", (totalDistance /1000)]; self->tempOldLocation=newLocation; 计算速度: CLLocationDistance d
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;
我想用公里/小时来计算速度。请告诉我以公里/小时为单位计算速度的任何方法。看起来你在将米除以秒,即米每秒。 将米转换为公里,将秒转换为小时
double calculatedSpeed = (distanceChange / 1000) / (sinceLastUpdate / 60 / 60)
您将直接从
CLLocation
的CLLocationSpeed
属性中获得速度,该属性是在cllocationmanager中的委派方法didUpdateLocation
中获得的
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let speed = locations.last?.speed else { return }
viewModel.speed.value = speed * 3600/1000
}
试试这个链接