I am using MKMapView on a project and would like to center the map on a coordinate and zoom in. Just like Google maps has:
GMSCameraPosition.camera(withLatitude:
This is working code tested on swift 4.2
override func viewDidLoad() {
super.viewDidLoad()
let initialLocation = CLLocation(latitude: 28.5761897, longitude: 77.172080)
self.centerMapOnLocation(location: initialLocation)
}
func centerMapOnLocation(location: CLLocation) {
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude), span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
DispatchQueue.main.async {
self.mapView.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location.coordinate
self.mapView.addAnnotation(annotation)
}
}
This will load with animation. Hope will help you.