你能举例说明如何计算当前的“速度”,我正在研究我的第一个简单应用程序ala里程表吗?
我想要使用didUpdateToLocation
我发现我需要使用公式speed = distance / duration
是不是?如何计算持续时间?
答案 0 :(得分:3)
您需要经历的基本步骤如下所示:
(可选)如果"速度" ISN'吨 设置,尝试从上次更新之间的距离计算它 和当前,除以时间戳的差异
import CoreLocation
class locationDelegate: NSObject, CLLocationManagerDelegate {
var last:CLLocation?
override init() {
super.init()
}
func processLocation(_ current:CLLocation) {
guard last != nil else {
last = current
return
}
var speed = current.speed
if (speed > 0) {
print(speed) // or whatever
} else {
speed = last!.distance(from: current) / (current.timestamp.timeIntervalSince(last!.timestamp))
print(speed)
}
last = current
}
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
for location in locations {
processLocation(location)
}
}
}
var del = locationDelegate()
var lm = CLLocationManager();
lm.delegate = del
lm.startUpdatingLocation()