iphone – 定位MKMapView以一次显示多个注释

我有几个注释,我想添加到我的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?

提前致谢。

从iOS7,你可以使用showAnnotations:animated:

[mapView showAnnotations:annotations animated:YES];
http://stackoverflow.com/questions/1336370/positioning-mkmapview-to-show-multiple-annotations-at-once

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:iphone – 定位MKMapView以一次显示多个注释