Using MKMapView, how can I center and zoom in on a coordinate as much as possible?

40 Views Asked by At

I have an MKMapView with various annotations. On tapping an annotation, I want the map to center and zoom in on that annotation as much as possible.

Using mapView.setCenter works to set the center, but not to zoom in.

Using mapView.setRegion with some region I define with my desired center location and span sort of works, but the span I set is largely arbitrary, and when it's too small, it seems that the mapView defaults to some other larger span from what I can see.

Is there a way for me to center on a specified location and zoom in as much as possible on that location programatically?

1

There are 1 best solutions below

0
son On

You can make the span exactly meters from center location. Try this:

private func zoomIn(_ location: CLLocationCoordinate2D, with metersFromCenter: Int = 0) {
    let region = MKCoordinateRegion(center: location,
                                    latitudinalMeters: CLLocationDistance(exactly: metersFromCenter)!,
                                    longitudinalMeters: CLLocationDistance(exactly: metersFromCenter)!)
    mapView.setRegion(mapView.regionThatFits(region), animated: true)
}

Output here, this is the maximum zoom level. Notice that this span level does not match with Apple Map default.

enter image description here