定位 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屋!