Created
February 10, 2011 15:19
-
-
Save volca/820686 to your computer and use it in GitHub Desktop.
automatic center map
This file contains 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
- (void)centerMap { | |
NSArray *coordinates = [self.mapView valueForKeyPath:@"annotations.coordinate"]; | |
CLLocationCoordinate2D maxCoord = {-90.0f, -180.0f}; | |
CLLocationCoordinate2D minCoord = {90.0f, 180.0f}; | |
for(NSValue *value in coordinates) { | |
CLLocationCoordinate2D coord = {0.0f, 0.0f}; | |
[value getValue:&coord]; | |
if(coord.longitude > maxCoord.longitude) { | |
maxCoord.longitude = coord.longitude; | |
} | |
if(coord.latitude > maxCoord.latitude) { | |
maxCoord.latitude = coord.latitude; | |
} | |
if(coord.longitude < minCoord.longitude) { | |
minCoord.longitude = coord.longitude; | |
} | |
if(coord.latitude < minCoord.latitude) { | |
minCoord.latitude = coord.latitude; | |
} | |
} | |
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}}; | |
region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0; | |
region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0; | |
region.span.longitudeDelta = maxCoord.longitude - minCoord.longitude; | |
region.span.latitudeDelta = maxCoord.latitude - minCoord.latitude; | |
[self.mapView setRegion:region animated:YES]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment