マップのズーム レベルを注釈のみを含むように設定しようとしていますが、たとえば半径 3 マイル以下はズームしませんが、注釈が画面の側面に触れないようにしています。さらに、一度ズームした注釈がself.viewの下半分に収まるようにしたいと思います(私のマップは画面全体を占めています)。
次の2つの方法を試しましたが、どちらも機能しません。
// #1 ///// テキサス州の注釈を表示するために常にズームし、遠くまでズームアウトすると、米国の州も表示されません
-(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 ///// これが最もうまく機能しますが、ズームインしすぎます。注釈が 1 つしかない場合は家の真上ですが、1 つの注釈と半径 4 マイルを見たいと思います。したがって、ユーザーが手動でズームしない限り、表示されている 4 平方マイル未満にズームしたくありません。
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];