我有几个要添加到我的 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
?
提前致谢。