我目前正在尝试获取当前用户的设备移动速度。这是我的代码:
import UIKit
import Foundation
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let manager = CLLocationManager()
var currentSpeed = 0.0
override func viewDidLoad() {
super.viewDidLoad()
manager.requestWhenInUseAuthorization()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
manager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(manager.location?.speed)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error: \(error.localizedDescription)")
}
唯一的问题是它会打印"Optional(-1,0)"
感谢您的帮助。
答案 0 :(得分:5)
如果设备没有移动,或者移动得足够慢以至于数学没有意义,核心位置会给你一个负速度值。