-
-
Save loganwright/d492ef532e3268295792 to your computer and use it in GitHub Desktop.
Map Region Encompassing Coords w/ Padding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (MKCoordinateRegion)regionForCoords:(NSArray *)coords withPaddingFactor:(CGFloat)paddingFactor { | |
CLLocationDegrees minLat = 90.0; | |
CLLocationDegrees maxLat = -90.0; | |
CLLocationDegrees minLon = 180.0; | |
CLLocationDegrees maxLon = -180.0; | |
for (NSValue *val in coords) { | |
CLLocationCoordinate2D coord = [val MKCoordinateValue]; | |
if (coord.latitude < minLat) { | |
minLat = coord.latitude; | |
} | |
if (coord.longitude < minLon) { | |
minLon = coord.longitude; | |
} | |
if (coord.latitude > maxLat) { | |
maxLat = coord.latitude; | |
} | |
if (coord.longitude > maxLon) { | |
maxLon = coord.longitude; | |
} | |
} | |
CLLocationDegrees latDelta = (maxLat - minLat); | |
CLLocationDegrees latPadding = latDelta * paddingFactor; | |
latDelta += latPadding; | |
CLLocationDegrees lonDelta = (maxLon - minLon); | |
CLLocationDegrees lonPadding = lonDelta * paddingFactor; | |
lonDelta += lonPadding; | |
CLLocationDegrees latitudeOffset = fabs(latDelta / 2.0); | |
CLLocationDegrees longitudeOffset = fabs(lonDelta / 2.0); | |
minLat -= latitudeOffset; | |
maxLat += latitudeOffset; | |
minLon -= longitudeOffset; | |
maxLon += longitudeOffset; | |
MKCoordinateSpan span = MKCoordinateSpanMake(latDelta, lonDelta); | |
CLLocationDegrees middleLatitude = (maxLat + minLat) / 2; | |
CLLocationDegrees middleLongitude = (maxLon + minLon) / 2; | |
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(middleLatitude, middleLongitude); | |
return MKCoordinateRegionMake(centerCoordinate, span); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment