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);
}
@MaciejGad
Copy link

Greate pice of code, it works like a charm ;)

@bendinwire
Copy link

bendinwire commented Feb 21, 2017

Here is a version of it for Swift 3:

func regionForAnnotations(annotations : [MKAnnotation]) ->MKCoordinateRegion {

        var minLat: CLLocationDegrees = 90.0
        var maxLat: CLLocationDegrees = -90.0
        var minLon: CLLocationDegrees = 180.0
        var maxLon: CLLocationDegrees = -180.0

        for annotation in annotations as [MKAnnotation] {
            let lat = Double(annotation.coordinate.latitude)
            let long = Double(annotation.coordinate.longitude)
            if (lat < minLat) {
                minLat = lat
            }
            if (long < minLon) {
                minLon = long
            }
            if (lat > maxLat) {
                maxLat = lat
            }
            if (long > maxLon) {
                maxLon = long
            }
        }

        let span = MKCoordinateSpanMake(maxLat - minLat, maxLon - minLon)

        let center = CLLocationCoordinate2DMake((maxLat - span.latitudeDelta / 2), maxLon - span.longitudeDelta / 2)

        return MKCoordinateRegionMake(center, span)
    }

@falkobuttler
Copy link

And a Swift 4 version that makes it a constructor on MKCoordinateRegion:

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 = MKCoordinateSpanMake(maxLat - minLat, maxLon - minLon)
        let center = CLLocationCoordinate2DMake((maxLat - span.latitudeDelta / 2), maxLon - span.longitudeDelta / 2)
        self.init(center: center, span: 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