I am trying to set the zoom level of the map to include just the annotations, but also don't zoom below say a 3 mile radius, but I also don't the annotations touching the side of the screens. Further, I would also like the annotations once zoomed to be within the bottom half self.view (my map takes up the entire screen).
I tried the following two ways and neither works.
// #1 ///// Always zooms to show my annotation in Texas, zoomed out to far I can't even see US states
-(void)zoomToFitMapAnnotations:(MKMapView*)aMapView {
if([aMapView.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){
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);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
//Add a little space on both sides
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
//Add a little extra space on bith sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [aMapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
}
// #2 ///// This works best, but it zooms in to much; right on top of a house, if there is only one annotation, but I want to see the one annotation and a 4 mile radius. So I never want to zoom less than having 4 square miles shown, UNLESS the user manually zooms it.
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in self.mapView.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
[self.mapView setVisibleMapRect:zoomRect edgePadding:UIEdgeInsetsMake(300, 300, 100, 300) animated:YES];