MKMapView visible region padding

Thanks to approach suggested above, complete solution is following.

import Foundation
import MapKit
extension MKCoordinateRegion{
    var mapRect:MKMapRect {
        get{
            let a = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                   self.center.latitude + self.span.latitudeDelta / 2,
                   self.center.longitude - self.span.longitudeDelta / 2))

            let b = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                    self.center.latitude - self.span.latitudeDelta / 2,
                    self.center.longitude + self.span.longitudeDelta / 2))

            return MKMapRectMake(min(a.x,b.x), min(a.y,b.y), abs(a.x-b.x), abs(a.y-b.y))
        }
    }
}

extension MKMapView {
    func setVisibleRegion(mapRegion: MKCoordinateRegion, edgePadding insets: UIEdgeInsets, animated animate: Bool) {
        self.setVisibleMapRect(mapRegion.mapRect, edgePadding: insets , animated: animate)
    }
}

Now you can just use setVisibleRegion func.


If you need a padding from the bottom of 100 in swift you could simply write:

mapView.layoutMargins = UIEdgeInsets(top: 10, right: 10, bottom: 100, left: 10)

Then you can check this centering the map on some annotations like this:

mapView.showAnnotations(mapView.annotations, animated: true)

If you print your visible region you will see that the span will be padded since it needs to fit the view. The center coordinate will still be in the center of the view. I'm not sure what you want to do but I guess it can be archived with

setVisibleMapRect:edgePadding:animated:

You need to have the region converted to a MKMapRect. See Convert MKCoordinateRegion to MKMapRect on how to do that