请问我如何计算当前速度的示例,我正在开发我的第一个简单的应用程序ala速度计?
Could you please give me example how to calculate current "speed", I'm working on my first simple app ala speedometer?
我想使用 didUpdateToLocation
我还发现我需要使用公式 speed = distance / duration /
also I found that I need to use formula speed = distance / duration
对吗?
推荐答案所需的基本步骤如下:
(可选),如果速度没有设置,请尝试根据上次更新与当前更新之间的距离除以时间戳记的差异来计算它。
(Optionally) if the "speed" isn't set, try calculating it from the distance between the last update and the current, divided by the difference in timestamps 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()
更多推荐
提供获取当前速度的简单方法(实施速度计)
发布评论