2012-05-24 6 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 = [[аннотации valueForKeyPath: @ "@ max.latitude"] doubleValue]; double lon_min = [[annotations valueForKeyPath: @ "@ min.longitude"] doubleValue]; double lon_max = [[аннотации valueForKeyPath: @ "@ max.longitude"] doubleValue]; – bigkm

Смежные вопросы