Skip to content

Instantly share code, notes, and snippets.

@robmooney
Created April 16, 2011 17:07
Show Gist options
  • Save robmooney/923301 to your computer and use it in GitHub Desktop.
Save robmooney/923301 to your computer and use it in GitHub Desktop.
Get the MKCoordinateRegion that encompasses a set of MKAnnotations
// returns a MKCoordinateRegion that encompasses an array of MKAnnotations
- (MKCoordinateRegion)regionForAnnotations:(NSArray *)annotations {
CLLocationDegrees minLat = 90.0;
CLLocationDegrees maxLat = -90.0;
CLLocationDegrees minLon = 180.0;
CLLocationDegrees maxLon = -180.0;
for (id <MKAnnotation> annotation in annotations) {
if (annotation.coordinate.latitude < minLat) {
minLat = annotation.coordinate.latitude;
}
if (annotation.coordinate.longitude < minLon) {
minLon = annotation.coordinate.longitude;
}
if (annotation.coordinate.latitude > maxLat) {
maxLat = annotation.coordinate.latitude;
}
if (annotation.coordinate.longitude > maxLon) {
maxLon = annotation.coordinate.longitude;
}
}
MKCoordinateSpan span = MKCoordinateSpanMake(maxLat - minLat, maxLon - minLon);
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((maxLat - span.latitudeDelta / 2), maxLon - span.longitudeDelta / 2);
return MKCoordinateRegionMake(center, span);
}
@dionc
Copy link

dionc commented Jan 25, 2018

Here is a version that correctly handles coordinates around the 180th meridian:
https://gist.github.com/dionc/46f7e7ee9db7dbd7bddec56bd5418ca6

@wzbozon
Copy link

wzbozon commented Nov 21, 2018

Swift 4.2 version of falkobuttler's version. Region is bigger so that coordinates are not at the edges:

import Foundation
import MapKit
extension MKCoordinateRegion {
    init(coordinates: [CLLocationCoordinate2D]) {
        var minLat: CLLocationDegrees = 90.0
        var maxLat: CLLocationDegrees = -90.0
        var minLon: CLLocationDegrees = 180.0
        var maxLon: CLLocationDegrees = -180.0
        
        for coordinate in coordinates {
            let lat = Double(coordinate.latitude)
            let long = Double(coordinate.longitude)
            if lat < minLat {
                minLat = lat
            }
            if long < minLon {
                minLon = long
            }
            if lat > maxLat {
                maxLat = lat
            }
            if long > maxLon {
                maxLon = long
            }
        }
        
        let span = MKCoordinateSpan(latitudeDelta: (maxLat - minLat)*2.0, longitudeDelta: (maxLon - minLon)*2.0)
        let center = CLLocationCoordinate2DMake(maxLat - span.latitudeDelta / 4, maxLon - span.longitudeDelta / 4)
        self.init(center: center, span: span)
    }
}

@sanju-naik-zz
Copy link

No need of this anymore, From iOS 7 We can directly use this Api to set the region so all the given annotations are visible.

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

@BrentMifsud
Copy link

No need of this anymore, From iOS 7 We can directly use this Api to set the region so all the given annotations are visible.

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

That fine if you want to zoom everything to fit in the center of the map. But sometimes you want to offset it (like if you have a bottom sheet that covers half the map).

You need to be able to get a region that fits the coordinates to offset it. So this is still valid code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment