#define MINIMUM_ZOOM_ARC
0.004
#define ANNOTATION_REGION_PAD_FACTOR_NARROW
2
#define MAX_DEGREES_ARC
360
- (void)zoomMapViewToFitAnnotations
:(NSMutableArray
<MKPointAnnotation
*>*)annotations animated
:(BOOL
)animated
{
long count
= [annotations count
];
if ( count
== 0) {
return;
}
MKMapPoint points
[count
];
for( int i
=0; i
<count
; i
++ ){
CLLocationCoordinate2D coordinate
= [(id
<MKAnnotation>)[annotations objectAtIndex
:i
] coordinate
];
points
[i
] = MKMapPointForCoordinate(coordinate
);
}
MKMapRect mapRect
= [[MKPolygon polygonWithPoints
:points count
:count
] boundingMapRect
];
MKCoordinateRegion region
= MKCoordinateRegionForMapRect(mapRect
);
region
.span
.latitudeDelta
*= ANNOTATION_REGION_PAD_FACTOR_NARROW
;
region
.span
.longitudeDelta
*= ANNOTATION_REGION_PAD_FACTOR_NARROW
;
if( region
.span
.latitudeDelta
> MAX_DEGREES_ARC
) {
region
.span
.latitudeDelta
= MAX_DEGREES_ARC
;
}
if( region
.span
.longitudeDelta
> MAX_DEGREES_ARC
){
region
.span
.longitudeDelta
= MAX_DEGREES_ARC
;
}
if( region
.span
.latitudeDelta
< MINIMUM_ZOOM_ARC
) {
region
.span
.latitudeDelta
= MINIMUM_ZOOM_ARC
;
}
if( region
.span
.longitudeDelta
< MINIMUM_ZOOM_ARC
) {
region
.span
.longitudeDelta
= MINIMUM_ZOOM_ARC
;
}
if(count
== 1) {
region
.span
.latitudeDelta
= MINIMUM_ZOOM_ARC
;
region
.span
.longitudeDelta
= MINIMUM_ZOOM_ARC
;
}
[self
.mapView setRegion
:region animated
:animated
];
}