3
 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];

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

4

1 回答 1

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];
于 2013-09-06T08:45:33.753 回答