我无法找到设备的高度,我需要在我的应用程序中显示海拔高度我该怎么做? 我试过了:
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()
}
没有出现......
答案 0 :(得分:3)
实际上你没有调用正确的委托方法,你正在调用:
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
}
我还建议您不要在此委托函数中调用locationManager.stopUpdatingLocation()
。
希望这会对你有所帮助!
注意:此语法适用于Swift 3.0,但我认为您只需删除Swift 2.2的委托函数中的_
。