MKMapView中心并缩放注释视图,

yab*_*zey 3 objective-c mkmapview mkannotation

 MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
 MKPointAnnotation *annotation1 = [[MKPointAnnotation alloc] init];
 MKPointAnnotation *annotation2 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *annotation3 = [[MKPointAnnotation alloc] init];
 MKPointAnnotation *annotation4 = [[MKPointAnnotation alloc] init];
 MKPointAnnotation *annotation5 = [[MKPointAnnotation alloc] init];
 MKPointAnnotation *annotation6 = [[MKPointAnnotation alloc] init];
annotation.coordinate=CLLocationCoordinate2DMake(40.748736,-73.892523);
annotation.title=@"Head Office";
annotation1.coordinate=CLLocationCoordinate2DMake(40.747972,-73.891858);
annotation1.title=@"Kalpana Chawla";
annotation2.coordinate=CLLocationCoordinate2DMake(40.74768,-73.891818);
annotation2.title=@"New Jackson Heights";
annotation3.coordinate=CLLocationCoordinate2DMake(40.642973,-73.979019);
annotation3.title=@"Brooklyn";
annotation4.coordinate=CLLocationCoordinate2DMake(40.617862,-73.962418);
annotation4.title=@"Coney Islands";
annotation5.coordinate=CLLocationCoordinate2DMake(40.83659,-73.853234);
annotation5.title=@"Bronx";
annotation6.coordinate=CLLocationCoordinate2DMake(40.635336,-73.963204);
annotation6.title=@"Malborough";

[mapView addAnnotation:annotation];
[mapView addAnnotation:annotation1];
[mapView addAnnotation:annotation2];
[mapView addAnnotation:annotation3];
[mapView addAnnotation:annotation4];
[mapView addAnnotation:annotation5];
[mapView addAnnotation:annotation6];
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations) {
    NSLog(@"%@",annotation);
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
    if (MKMapRectIsNull(zoomRect)) {
        zoomRect = pointRect;
    } else {
        zoomRect = MKMapRectUnion(zoomRect, pointRect);
    }
}
[mapView setVisibleMapRect:zoomRect animated:YES];
Run Code Online (Sandbox Code Playgroud)

我有这六个静态位置,我必须在MK Map上显示.在手动声明所有注释之后,我使用了在stackoverflow上找到的for循环代码来居中并将我的地图缩放到声明的注释.但是当我运行我的应用程序时,地图会给我一个朝向大西洋的默认缩放级别视图.请帮忙.我在模拟器中运行应用程序

kar*_*ika 13

MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations) {
    NSLog(@"%@",annotation);
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
    if (MKMapRectIsNull(zoomRect)) {
        zoomRect = pointRect;
    } else {
        zoomRect = MKMapRectUnion(zoomRect, pointRect);
    }
}
[mapView setVisibleMapRect:zoomRect animated:YES];
Run Code Online (Sandbox Code Playgroud)