Swift 3.0
In the MapKit function didUpdateLocations:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last as CLLocation? else { return }
let userCenter = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: userCenter, span: MKCoordinateSpan(latitudeDelta: 180, longitudeDelta: 180))
mkView.setRegion(region, animated: true)
}
Note. If you don’t want to constantly install the center every time you update your location, follow these steps:
let userCenter = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
// Does not have to be userCenter, could replace latitude: and longitude: with any value you would like to center in on
let region = MKCoordinateRegion(center: userCenter, span: MKCoordinateSpan(latitudeDelta: 180, longitudeDelta: 180))
mkView.setRegion(region, animated: true)
source
share