MKMapView center and zoom in

后端 未结 9 1702
一向
一向 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:33

    This works in swift 5, ios 13 and is easy to understand:

    Just add the following method to your class:

    func zoomAndCenter(on centerCoordinate: CLLocationCoordinate2D, zoom: Double) {
        var span: MKCoordinateSpan = mapView.region.span
        span.latitudeDelta *= zoom
        span.longitudeDelta *= zoom
        let region: MKCoordinateRegion = MKCoordinateRegion(center: centerCoordinate, span: span)
        mapView.setRegion(region, animated: true)
    }
    

提交回复
热议问题