I use this code to show all my annotations on my map:
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 1000);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[mapView setVisibleMapRect:zoomRect animated:YES];
But my problem is that when the annotations are close to each other, it zooms too much as the rectangle is small.
Any way to fix this?
For those of us who like one liners:
In my code, I add extra spacing around, so it will automatically adjust the zoom level in order to fit.
Ariel's solution wasn't working for me either but n00neimp0rtant's was. I have rewritten it in Swift as a function
rectWithMinimumZoom(_ minimumZoom: Double)
in an extension of MKMapRect. In return it the adjusted rect (if adjustment is needed). Also I added a extra safety that minimumZoom cannot be divided by 0.SWIFT
Try this code:
EDITED ->
Ariel's answer didn't work for me, but I made a few small changes to it and it's working great now (especially with maps with a single pin):