看看這裏Finding Altitude in Swift 再看看我的代碼核心位置返回0海拔
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≦
你的高度是多少?它返回什麼經緯度?地點是否爲零?實際上是否有位置?你在模擬器上的設備上運行嗎? – Fogmeister
它快3嗎? –
是的,我必須在swift中編寫代碼3 –