Created
May 14, 2014 19:53
-
-
Save trungtran/0cc3dc4006b7052bce11 to your computer and use it in GitHub Desktop.
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)zoomToFitMapAnnotations:(MKMapView*)mapView | |
{ | |
if([mapView.annotations count] == 0) | |
return; | |
CLLocationCoordinate2D topLeftCoord; | |
topLeftCoord.latitude = -90; | |
topLeftCoord.longitude = 180; | |
CLLocationCoordinate2D bottomRightCoord; | |
bottomRightCoord.latitude = 90; | |
bottomRightCoord.longitude = -180; | |
for(id<MKAnnotation> annotation in self.mapView.annotations) | |
{ | |
if ([annotation isKindOfClass:[MKUserLocation class]]) { | |
continue; | |
} | |
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); | |
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); | |
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); | |
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); | |
} | |
MKMapPoint topLeftCoordMapPoint = MKMapPointForCoordinate(topLeftCoord); | |
MKMapPoint bottomRightCoordMapPoint = MKMapPointForCoordinate(bottomRightCoord); | |
MKMapRect rect = MKMapRectMake(topLeftCoordMapPoint.x, | |
topLeftCoordMapPoint.y, | |
bottomRightCoordMapPoint.x - topLeftCoordMapPoint.x, | |
bottomRightCoordMapPoint.y - topLeftCoordMapPoint.y); | |
rect = [[self mapView] mapRectThatFits:rect edgePadding:UIEdgeInsetsMake(self.topLayoutGuide.length + 20, 0.0, self.tableView.frame.size.height + 30, 0.0)]; | |
[mapView setVisibleMapRect:rect animated:YES]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment