gpt4 book ai didi

ios - 如何在 ios 8 中获取当前高度?

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

我无法找到设备的海拔高度,我需要在我的应用程序中显示海拔高度我该怎么做?我试过了:

 var locationManager:CLLocationManager = CLLocationManager()

override func viewDidLoad() {

super.viewDidLoad()
self.locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
self.locationManager.delegate = self
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()

}


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

没有显示....

最佳答案

实际上你没有调用正确的委托(delegate)方法,你调用的是:locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) 但能给你想要的是 locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]),

因此,如果您确实将 NSLocationWhenInUseUsageDescription 键实现到您的 Info.plist 文件中,并且如果弹出窗口显示您只需要删除您的函数并添加这个函数,它应该工作:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let lastLocation = locations.last else {
NSLog("error no last location")
return
}
let altitude = lastLocation.altitude
// Do what you want with your altitude
}

我还建议您不要在此委托(delegate)函数内调用 locationManager.stopUpdatingLocation()

希望对您有所帮助!

注意:此语法适用于 Swift 3.0,但我认为您只需删除 Swift 2.2 的委托(delegate)函数中的 _

关于ios - 如何在 ios 8 中获取当前高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38576338/

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