【发布时间】:2017-04-20 00:35:15
【问题描述】:
看这里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?
【问题讨论】:
-
你的海拔是多少?它返回什么纬度和经度?位置为零吗?真的有位置吗?您是否在模拟器上的设备上运行?
-
是swift 3吗?
-
您实现了
CLLocationManagerDelegate协议吗?我的意思是......保存每个并发位置?每次位置更新都会更加准确。 -
你是在真机而不是模拟器上测试吗?另外,您有位置(纬度/经度)吗?
标签: swift swift3 core-location