gpt4 book ai didi

swift - CoreLocation 用户速度

转载 作者:搜寻专家 更新时间:2023-10-31 08:26:11 26 4
gpt4 key购买 nike

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

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

关于swift - CoreLocation 用户速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32871750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com