CLLocation speed return -1.0

社会主义新天地 提交于 2019-12-12 03:52:20

问题


I have problem. My GPS (on iPad mini 2 in Wifi and on iPhone 6 in 3G/4G) the speed return -1.0. Have an idea? This what i receive in console log:

Long: 12.5245, Lat: 41.9456, Speed:-1.0, kph: -3.6

Here the code in didUpdateLocations()

    let userLocation: CLLocation = locations[0]
    var speed: CLLocationSpeed = CLLocationSpeed()
    speed = (locationManager.location?.speed)!

    SpeedLabel.text = String(format: "%.0f km/h", speed * 3.6)
    let long = String(Float(userLocation.coordinate.longitude))
    let lat = String(Float(userLocation.coordinate.latitude))


    print("Long: \(long), Lat: \(lat), Speed:\(speed), kph: \(speed * 3.6) ")

回答1:


I had this problem too. A negative value means an invalid speed. This is most of the time occured when you're inside a building and your location is moving a lot due to the building.

A simple fix would be:

if speed < 0 { 
    speed = 0 
}

This checks if the speed is negative. If it is, it'll reset it to 0.



来源:https://stackoverflow.com/questions/39172130/cllocation-speed-return-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!