当前位置 : 主页 > 手机开发 > 其它 >

swift – CoreLocation用户速度

来源:互联网 收集:自由互联 发布时间:2021-06-12
我目前正在尝试获取当前用户的设备移动速度.这是我的代码: import UIKitimport Foundationimport CoreLocationclass ViewController: UIViewController, CLLocationManagerDelegate {let manager = CLLocationManager()var curre
我目前正在尝试获取当前用户的设备移动速度.这是我的代码:

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

网友评论