我正在尝试启动简单的地理位置应用,但它正在构建没有错误,它不会更新我应该的位置数据。我正在使用swift 3,xcode 8
class CurrentLocationViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var location: CLLocation?
@IBOutlet weak var messageLabel: UILabel!
@IBOutlet weak var latitudeLabel: UILabel!
@IBOutlet weak var longitudeLabel: UILabel!
@IBOutlet weak var tagButton: UIButton!
@IBAction func getMyLocation(_ sender: Any) {
let authStatus = CLLocationManager.authorizationStatus()
if authStatus == .notDetermined {
locationManager.requestWhenInUseAuthorization()
return
}
if authStatus == .denied || authStatus == .restricted {
showLocationServicesDeniedAlert()
return
}
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
updateLabels()
}
//Showing Alert message if location service is disabled
func showLocationServicesDeniedAlert() {
let alert = UIAlertController(title: "Location Services Disabled", message: "Please enable location services for this app in Settings.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
present(alert, animated: true, completion: nil)
}
//Updating Labels if Location is tutned on
func updateLabels() {
if let location = location {
latitudeLabel.text = String (format: "%.8f", location.coordinate.latitude)
longitudeLabel.text = String (format: "%.8f", location.coordinate.longitude)
tagButton.isHidden = false
messageLabel.text = ""
} else {
latitudeLabel.text = ""
longitudeLabel.text = ""
tagButton.isHidden = true
messageLabel.text = "Tap 'Get My Location' to Start"
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - CLLocationManagerDelegate
func locationManager (manager: CLLocationManager, didFailWithError error: NSError) {
print("didFailWithError \(error)")
}
func locationManager (manager: CLLocationManager, didUpdateLocations locations : [CLLocation]) {
let newLocation = locations.last!
print("didUpdateLocations \(newLocation)")
location = newLocation
updateLabels()
}
}
我甚至无法在调试过程中发现错误,因为当我尝试在控制台中编写print时,没有任何反应。