MKMapView center and zoom in

后端 未结 9 1726
一向
一向 2021-02-19 23:16

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:         


        
9条回答
  •  半阙折子戏
    2021-02-19 23:45

    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.

提交回复
热议问题