Skip to content

Instantly share code, notes, and snippets.

@trungtran
Created May 14, 2014 19:53
Show Gist options
  • Save trungtran/0cc3dc4006b7052bce11 to your computer and use it in GitHub Desktop.
Save trungtran/0cc3dc4006b7052bce11 to your computer and use it in GitHub Desktop.
-(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