gpt4 book ai didi

swift - 核心位置返回 0 作为高度

转载 作者:搜寻专家 更新时间:2023-11-01 05:35:43 24 4
gpt4 key购买 nike

看这里Finding Altitude in Swift看看我的代码

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var lbl: UILabel!
var player: AVAudioPlayer?

var locationManager:CLLocationManager = CLLocationManager()

var alt: Double?

@IBOutlet weak var coor: UILabel!

func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
alt = newLocation.altitude
var altitude = locationManager.location?.altitude
print("\(altitude)")
manager.stopUpdatingLocation()
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
self.locationManager.delegate = self
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()
}

@IBAction func getCoor(_ sender: Any) {
var altitude = locationManager.location?.altitude
coor.text = "\(altitude)"
}
}

为什么它不显示实际高度而是总是显示 0.0?

最佳答案

这种代码注定要失败:

func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
// ...
manager.stopUpdatingLocation()
}

使用 startUpdatingLocation 启动位置管理器后,您将在第一次调用 didUpdateLocation 时停止位置管理器。但第一次调用总是虚假的,甚至接下来的十几次调用也可能只是硬件获得修复时的预热。

如果您的问题是“我在哪里”,请使用 requestLocation,而不是 startUpdatingLocation。它只调用 didUpdateLocation 一次,而且只有当它确实 确实 有足够好的位置时(这可能需要一些时间)。

最后,请注意您的代码在 Swift 3 中不正确。locationManager(manager:...) 永远不会 被调用。 Read the docs学习正确的签名。

关于swift - 核心位置返回 0 作为高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40977276/

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