定位MKMapView以一次显示多个注释 [英] Positioning MKMapView to show multiple annotations at once
问题描述
我有几个注释,我想添加到我的MKMapView(它可以0-n项,其中n通常约为5)。我可以添加注释,但我想调整地图的大小,以适应屏幕上的所有注释,我不知道如何这样做。
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.
我一直在看着 -regionThatFits:
但我不知道该怎么办。我会发布一些代码,以显示我到目前为止。我认为这应该是一个通常很简单的任务,但我感觉有点不知所措的MapKit到目前为止。
I've been looking at -regionThatFits:
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.
- (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
?
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? -viewDidLoad
?
提前感谢。
推荐答案
从iOS7开始,您可以使用 showAnnotations:animated:
As of iOS7 you can use showAnnotations:animated:
[mapView showAnnotations:annotations animated:YES];
这篇关于定位MKMapView以一次显示多个注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!