title: Swift print center of map when user moves the map
body: <p>I want to draw a map with Swift under iOS 11. I would like that, when app starts, the map is centered on the position of the user.</p>
  
  <p>The output of the console, when I start the app is:</p>
  
  <pre><code>locations = 0.0 0.0
  locations = 49.8375375633452 3.45672192960001
  locations = 0.0 0.0
  locations = 49.8375375633452 3.45672192960001
  </code></pre>
  
  <p>Another problem occurs when I move the map with my finger, I would like to print the coordinates of the center of the map (and if possible, the region displayed on the screen) but nothing happens.</p>
  
  <p>Here is my complete code of the class.</p>
  
  <pre><code>import UIKit
  import MapKit
  
  class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
  
  
  
      var locationManager = CLLocationManager()
      func checkLocationAuthorizationStatus() {
          if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
              mapView.showsUserLocation = true
          } else {
              locationManager.requestWhenInUseAuthorization()
          }
      }
  
      override func viewDidAppear(_ animated: Bool) {
          super.viewDidAppear(animated)
          checkLocationAuthorizationStatus()
      }
  
  
      @IBOutlet weak var mapView: MKMapView!
  
  
      var currentLocation = CLLocation()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          //For use when the app is open
          locationManager.requestWhenInUseAuthorization()
          // If location services is enabled get the users location
          if CLLocationManager.locationServicesEnabled() {
              locationManager.delegate = self
              locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters // You can change the locaiton accuary here.
              locationManager.startUpdatingLocation()
          }
  
          mapView.delegate = self
          mapView.register(MyClassMarkerView.self,
                           forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
          centerMapOnLocation(location: self.currentLocation.coordinate)
  
      }
  
      let regionRadius: CLLocationDistance = 1000
  
      func centerMapOnLocation(location: CLLocationCoordinate2D) {
          let coordinateRegion = MKCoordinateRegionMakeWithDistance(location,
                                                                    regionRadius, regionRadius)
          print("locations = \(location.latitude) \(location.longitude)")
          mapView.setRegion(coordinateRegion, animated: true)
      }
  
  
      // Print out the location to the console
      func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          if let location = locations.first {
              self.currentLocation = location            
              print("locations = \(location.coordinate.latitude) \(location.coordinate.longitude)")
              locationManager.stopUpdatingLocation()
          }
      }
  
  
      // If we have been deined access give the user the option to change it
      func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
          if(status == CLAuthorizationStatus.denied) {
              showLocationDisabledPopUp()
          }
      }
  
      // Show the popup to the user if we have been deined access
      func showLocationDisabledPopUp() {
          let alertController = UIAlertController(title: "Background Location Access Disabled",
                                                  message: "In order to deliver pizza we need your location",
                                                  preferredStyle: .alert)
  
          let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
          alertController.addAction(cancelAction)
  
          let openAction = UIAlertAction(title: "Open Settings", style: .default) { (action) in
              if let url = URL(string: UIApplicationOpenSettingsURLString) {
                  UIApplication.shared.open(url, options: [:], completionHandler: nil)
              }
          }
          alertController.addAction(openAction)
  
          self.present(alertController, animated: true, completion: nil)
      }
  
      func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
                   calloutAccessoryControlTapped control: UIControl) {
          let location = view.annotation as! myClass
          let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking]
          location.mapItem().openInMaps(launchOptions: launchOptions)
      }
  
      func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
          print ("Here we are")
          var center = mapView.centerCoordinate
          print ("Center is \(center)")
      }
  
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>Thanks for your help.</p>