swift – CoreLocation用户速度

我目前正在尝试获取当前用户的设备移动速度.这是我的代码:

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)”

我在这里先向您的帮助表示感谢.

最佳答案 如果设备没有移动,或者移动得足够慢以至于数学没有意义,Core Location会给你一个负速度值.

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocation_Class/#//apple_ref/occ/instp/CLLocation/speed

点赞