我有几个注释要添加到 MKMapView 中(它可以是0-n 条目,其中 n 通常在5左右)。我可以很好地添加注释,但是我想调整地图的大小,以便一次性将所有注释放到屏幕上,而且我不确定如何做到这一点。
我一直在看 -regionThatFits:
,但我不太确定该怎么处理它。我将发布一些代码来显示我目前已经得到了什么。我认为这应该是一个通常直接的任务,但我感到有点不知所措与 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];
}
注意,这一切都发生在我收到一个位置更新... 我不知道这是否是一个合适的地方这样做。如果没有,哪里会是一个更好的地方?-viewDidLoad
?
先谢谢你。