2012-05-24 36 views

回答

7

假設你把所有的註解,你可以做以下的數組:

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]; 
} 
+0

就像一個魅力。非常感謝 – BlackMouse

+2

爲了節省你的循環,你可以加倍lat_min = [[annotations valueForKeyPath:@「@ min.latitude」] doubleValue]; double lat_max = [[annotations valueForKeyPath:@「@ max.latitude」] doubleValue]; double lon_min = [[annotations valueForKeyPath:@「@ min.longitude」] doubleValue]; double lon_max = [[annotations valueForKeyPath:@「@ max.longitude」] doubleValue]; – bigkm

相關問題