I have the following code to set an MKCoordinateRegion to an MKMapView:
override func viewDidLoad() {
super.viewDidLoad()
mapView.showsUserLocation = true
mapView.setUserTrackingMode(.followWithHeading, animated: true)
currentLocationCoordinateRegion = MKCoordinateRegion(center: mapView.userLocation.coordinate, latitudinalMeters: 25, longitudinalMeters: 25)
mapView.setRegion(currentLocationCoordinateRegion, animated: true)
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.distanceFilter = kCLDistanceFilterNone
let status = CLLocationManager.authorizationStatus()
if status == .authorizedAlways || status == .authorizedWhenInUse, status == .restricted {
} else {
locationManager.requestAlwaysAuthorization()
}
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
if currentLocationCoordinateRegion == nil {
currentLocationCoordinateRegion = MKCoordinateRegion(center: mapView.userLocation.coordinate, latitudinalMeters: 25, longitudinalMeters: 25)
mapView.setRegion(currentLocationCoordinateRegion, animated: true)
}
}
However, the map view shows a region that is much larger than 20 meters by 20 meters.
What is the problem and how do I fix it?
I tried running that code after the first time it is run, and the second time it works. It just doesn't zoom completely the first time.