我发现这个解决方案是最可靠的,@Anna 也提出了建议,所以它可能是一个不错的解决方案。
这是我的方法(实现为我继承的方法MKMapView
- (void)fitAnnotationsKeepingCenter {
CLLocation *centerLocation = [[CLLocation alloc]
initWithLatitude:self.centerCoordinate.latitude
longitude:self.centerCoordinate.longitude];
// starting distance (do not zoom less than this)
CLLocationDistance maxDistance = 350;
for (id <MKAnnotation> vehicleAnnotation in [self annotations]) {
CLLocation *annotationLocation = [[CLLocation alloc] initWithLatitude:vehicleAnnotation.coordinate.latitude longitude:vehicleAnnotation.coordinate.longitude];
maxDistance = MAX(maxDistance, [centerLocation distanceFromLocation:annotationLocation]);
}
MKCoordinateRegion fittedRegion = MKCoordinateRegionMakeWithDistance(centerLocation.coordinate, maxDistance * 2, maxDistance * 2);
fittedRegion = [self regionThatFits:fittedRegion];
fittedRegion.span.latitudeDelta *= 1.2;
fittedRegion.span.longitudeDelta *= 1.2;
[self setRegion:fittedRegion animated:YES];
}