Iphone MKMapKit。居中地图以显示所有注释

Iphone MKMapKit。居中地图以显示所有注释,iphone,objective-c,ios,mkmapview,mapkit,Iphone,Objective C,Ios,Mkmapview,Mapkit,我在地图上有2-10个图钉。当我单击一个按钮时,我希望地图缩小并居中,以便用户可以看到所有的图钉。我该怎么做 提前感谢假设您有一个包含所有注释的数组,您可以执行以下操作: CLLocationCoordinate2D leftTop = CLLocationCoordinate2DMake(-90,180); CLLocationCoordinate2D rightBottom = CLLocationCoordinate2DMake(90, -180); for (int i=0; i &l

我在地图上有2-10个图钉。当我单击一个按钮时,我希望地图缩小并居中,以便用户可以看到所有的图钉。我该怎么做


提前感谢

假设您有一个包含所有注释的数组,您可以执行以下操作:

CLLocationCoordinate2D leftTop = CLLocationCoordinate2DMake(-90,180);
CLLocationCoordinate2D rightBottom = CLLocationCoordinate2DMake(90, -180);

for (int i=0; i < [annotations count]; i++) {
    id<MKAnnotation> annotation = (id<MKAnnotation>)[annotation objectAtIndex:i];
    CLLocationCoordinate2D coord = annotation.coordinate;
    if (coord.latitude > leftTop.latitude) {
        leftTop.latitude = coord.latitude;
    }
    if (coord.longitude < leftTop.longitude) {
        leftTop.longitude = coord.longitude;
    }
    if (coord.latitude < rightBottom.latitude) {
        rightBottom.latitude = coord.latitude;
    }
    if (coord.longitude > rightBottom.longitude) {
        rightBottom.longitude = coord.longitude;
    }
}

MKCoordinateSpan regSpan = MKCoordinateSpanMake(leftTop.latitude-rightBottom.latitude, rightBottom.longitude-leftTop.longitude);
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(leftTop.latitude-regSpan.latitudeDelta/2, leftTop.longitude+regSpan.longitudeDelta/2);
regSpan.latitudeDelta = MAX(regSpan.latitudeDelta, 0.01);
regSpan.longitudeDelta = MAX(regSpan.longitudeDelta, 0.01);
MKCoordinateRegion reg = MKCoordinateRegionMake(center, regSpan);
if (CLLocationCoordinate2DIsValid(center)) {
    [_mapView setRegion:reg animated:YES];
}
CLLocationCoordinate2D leftTop=CLLocationCoordinate2DMake(-90180);
CLLocationCoordinate2D rightBottom=CLLocationCoordinate2DMake(90,-180);
对于(int i=0;i<[注释计数];i++){
id annotation=(id)[annotation objectAtIndex:i];
CLLocationCoordinate2D坐标=annotation.coordinate;
if(坐标纬度>左上角纬度){
leftTop.latitude=坐标纬度;
}
if(坐标经度<左上经度){
leftTop.longide=coord.longide;
}
if(坐标纬度<右下角纬度){
rightBottom.latitude=坐标纬度;
}
if(坐标经度>右下角经度){
rightBottom.longide=坐标经度;
}
}
MKCoordinateSpan regSpan=MKCoordinateSpanMake(leftTop.latitude-righbottom.latitude,righbottom.longitude-leftTop.longitude);
CLLocationCoordinate2D center=CLLocationCoordinate2DMake(leftTop.latitude-regSpan.latitudeDelta/2,leftTop.longitude+regSpan.longitudeDelta/2);
regSpan.latitudeDelta=最大值(regSpan.latitudelta,0.01);
regSpan.longitudeDelta=最大值(regSpan.longitudeDelta,0.01);
MKCoordinateRegion reg=MKCoordinateRegionMake(中心,regSpan);
if(CLLocationCoordination2无效(中间)){
[_MapViewSetRegion:reg动画:是];
}

假设您有一个包含所有注释的数组,您可以执行以下操作:

CLLocationCoordinate2D leftTop = CLLocationCoordinate2DMake(-90,180);
CLLocationCoordinate2D rightBottom = CLLocationCoordinate2DMake(90, -180);

for (int i=0; i < [annotations count]; i++) {
    id<MKAnnotation> annotation = (id<MKAnnotation>)[annotation objectAtIndex:i];
    CLLocationCoordinate2D coord = annotation.coordinate;
    if (coord.latitude > leftTop.latitude) {
        leftTop.latitude = coord.latitude;
    }
    if (coord.longitude < leftTop.longitude) {
        leftTop.longitude = coord.longitude;
    }
    if (coord.latitude < rightBottom.latitude) {
        rightBottom.latitude = coord.latitude;
    }
    if (coord.longitude > rightBottom.longitude) {
        rightBottom.longitude = coord.longitude;
    }
}

MKCoordinateSpan regSpan = MKCoordinateSpanMake(leftTop.latitude-rightBottom.latitude, rightBottom.longitude-leftTop.longitude);
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(leftTop.latitude-regSpan.latitudeDelta/2, leftTop.longitude+regSpan.longitudeDelta/2);
regSpan.latitudeDelta = MAX(regSpan.latitudeDelta, 0.01);
regSpan.longitudeDelta = MAX(regSpan.longitudeDelta, 0.01);
MKCoordinateRegion reg = MKCoordinateRegionMake(center, regSpan);
if (CLLocationCoordinate2DIsValid(center)) {
    [_mapView setRegion:reg animated:YES];
}
CLLocationCoordinate2D leftTop=CLLocationCoordinate2DMake(-90180);
CLLocationCoordinate2D rightBottom=CLLocationCoordinate2DMake(90,-180);
对于(int i=0;i<[注释计数];i++){
id annotation=(id)[annotation objectAtIndex:i];
CLLocationCoordinate2D坐标=annotation.coordinate;
if(坐标纬度>左上角纬度){
leftTop.latitude=坐标纬度;
}
if(坐标经度<左上经度){
leftTop.longide=coord.longide;
}
if(坐标纬度<右下角纬度){
rightBottom.latitude=坐标纬度;
}
if(坐标经度>右下角经度){
rightBottom.longide=坐标经度;
}
}
MKCoordinateSpan regSpan=MKCoordinateSpanMake(leftTop.latitude-righbottom.latitude,righbottom.longitude-leftTop.longitude);
CLLocationCoordinate2D center=CLLocationCoordinate2DMake(leftTop.latitude-regSpan.latitudeDelta/2,leftTop.longitude+regSpan.longitudeDelta/2);
regSpan.latitudeDelta=最大值(regSpan.latitudelta,0.01);
regSpan.longitudeDelta=最大值(regSpan.longitudeDelta,0.01);
MKCoordinateRegion reg=MKCoordinateRegionMake(中心,regSpan);
if(CLLocationCoordination2无效(中间)){
[_MapViewSetRegion:reg动画:是];
}

工作起来很有魅力。非常感谢您为您节省时间,因为对于loop,您可以将lat_min=[[annotations valueForKeyPath:@“@min.latitude”]doubleValue]加倍;双纬度最大值=[[valueForKeyPath:@“@max.latitude”]doubleValue];double lon_min=[[annotations valueForKeyPath:@“@min.longide”]double value];double lon_max=[[annotations valueForKeyPath:@“@max.longide”]double value];工作起来很有魅力。非常感谢您为您节省时间,因为对于loop,您可以将lat_min=[[annotations valueForKeyPath:@“@min.latitude”]doubleValue]加倍;双纬度最大值=[[valueForKeyPath:@“@max.latitude”]doubleValue];double lon_min=[[annotations valueForKeyPath:@“@min.longide”]double value];double lon_max=[[annotations valueForKeyPath:@“@max.longide”]double value];