swift – CoreLocation用户速度
发布时间:2020-12-14 04:26:42 所属栏目:百科 来源:网络整理
导读:我目前正在尝试获取当前用户的设备移动速度.这是我的代码: import UIKitimport Foundationimport CoreLocationclass ViewController: UIViewController,CLLocationManagerDelegate {let manager = CLLocationManager()var currentSpeed = 0.0override func
|
我目前正在尝试获取当前用户的设备移动速度.这是我的代码:
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
