Pozycjonowanie MKMapView, aby wyświetlić wiele adnotacji na raz

Mam kilka adnotacji, które chcę dodać do mojego MKMapView (może to być 0-n elementów, gdzie n wynosi ogólnie około 5). Mogę dodawać adnotacje w porządku, ale chcę zmienić rozmiar mapy, aby dopasować wszystkie adnotacje na ekranie naraz, i nie wiem, jak to zrobić.

Patrzyłem na-regionThatFits: ale nie jestem pewien, co z tym zrobić. Opublikuję kod, aby pokazać, co mam do tej pory. Myślę, że powinno to być ogólnie proste zadanie, ale jak dotąd czuję się nieco przytłoczony MapKit.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

location = newLocation.coordinate;
//One location is obtained.. just zoom to that location

MKCoordinateRegion region;
region.center = location;

//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta = 0.015;
span.longitudeDelta = 0.015;
region.span = span;
// Set the region here... but I want this to be a dynamic size
// Obviously this should be set after I've added my annotations
[mapView setRegion:region animated:YES];

// Test data, using these as annotations for now
NSArray *arr = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];
float ex = 0.01;
for (NSString *s in arr) {
    JBAnnotation *placemark = [[JBAnnotation alloc] initWithLat:(location.latitude + ex) lon:location.longitude];
    [mapView addAnnotation:placemark];
    ex = ex + 0.005;
}
    // What do I do here?
    [mapView setRegion:[mapView regionThatFits:region] animated:YES];
}

Zauważ, że wszystko dzieje się, gdy otrzymam aktualizację lokalizacji ... Nie wiem, czy to jest odpowiednie miejsce, aby to zrobić. Jeśli nie, gdzie byłoby lepsze miejsce?-viewDidLoad?

Z góry dziękuję.

questionAnswers(22)

yourAnswerToTheQuestion