Note that there are some explanatory texts on larger screens.

plurals
  1. POPositioning MKMapView to show multiple annotations at once
    text
    copied!<p>I've got several annotations I want to add to my MKMapView (it could 0-n items, where n is generally around 5). I can add the annotations fine, but I want to resize the map to fit all annotations onscreen at once, and I'm not sure how to do this.</p> <p>I've been looking at <code>-regionThatFits:</code> but I'm not quite sure what to do with it. I'll post some code to show what I've got so far. I think this should be a generally straightforward task but I'm feeling a bit overwhelmed with MapKit so far.</p> <pre><code>- (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]; } </code></pre> <p>Notice, this all happens as I receive a location update... I don't know if that's an appropriate place to do this. If not, where would be a better place? <code>-viewDidLoad</code>?</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload