我有幾個要添加到我的MKMapView的注釋(它可以是0-n個項目,其中n通常約為5)。我可以很好地添加注釋,但是我想立即調整地圖的大小以適合屏幕上的所有注釋,而且我不確定該怎么做。我一直在看,-regionThatFits:但是我不確定該怎么做。我將發(fā)布一些代碼以顯示到目前為止的內容。我認為這應該是一項通常很簡單的任務,但到目前為止,我對MapKit感到有些不知所措。- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{location = newLocation.coordinate;//One location is obtained.. just zoom to that locationMKCoordinateRegion region;region.center = location;//Set Zoom level using SpanMKCoordinateSpan 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 nowNSArray *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];}請注意,這一切都是在我收到位置更新信息時發(fā)生的...我不知道這是否合適。如果沒有,哪里會有更好的地方?-viewDidLoad?提前致謝。
4 回答

縹緲止盈
TA貢獻2041條經驗 獲得超4個贊
從iOS7開始,您可以使用showAnnotations:animated:
[mapView showAnnotations:annotations animated:YES];

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
為什么這么復雜?
MKCoordinateRegion coordinateRegionForCoordinates(CLLocationCoordinate2D *coords, NSUInteger coordCount) {
MKMapRect r = MKMapRectNull;
for (NSUInteger i=0; i < coordCount; ++i) {
MKMapPoint p = MKMapPointForCoordinate(coords[i]);
r = MKMapRectUnion(r, MKMapRectMake(p.x, p.y, 0, 0));
}
return MKCoordinateRegionForMapRect(r);
}
- 4 回答
- 0 關注
- 893 瀏覽
添加回答
舉報
0/150
提交
取消