2
你能给我例子如何计算当前的“速度”,我正在研究我的第一个简单的应用程序ala车速表?提供简单的方法来获得当前的速度(执行速度计)
我想出用didUpdateToLocation
我也发现,我需要使用公式speed = distance/duration
是不是?如何计算持续时间?
你能给我例子如何计算当前的“速度”,我正在研究我的第一个简单的应用程序ala车速表?提供简单的方法来获得当前的速度(执行速度计)
我想出用didUpdateToLocation
我也发现,我需要使用公式speed = distance/duration
是不是?如何计算持续时间?
,你需要经过像这样的基本步骤:
(可选)如果”速度“不是 集合,请尝试从las之间的距离计算它T更新的 和电流,通过差异时间戳分
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()
人,什么是* *了今天的堆栈溢出编辑器?这取得了太多的编辑,使格式正确。 –
我试图实现类似的这一点。你能解释一下差距吗?只是从速度上获得速度并使用公式计算它?因为当我打印两种速度方式他们有稍微不同的值 – Ilya
我的问题是在真实的设备上,当我开车时,速度是不正确的。我在'didUpdateLocations'中使用了'locations.speed'。 – Ilya