猿问

定位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?


提前致谢。


慕虎7371278
浏览 869回答 4
4回答

缥缈止盈

从iOS7开始,您可以使用showAnnotations:animated:[mapView showAnnotations:annotations animated:YES];

翻翻过去那场雪

为什么这么复杂?MKCoordinateRegion coordinateRegionForCoordinates(CLLocationCoordinate2D *coords, NSUInteger coordCount) {&nbsp; &nbsp; MKMapRect r = MKMapRectNull;&nbsp; &nbsp; for (NSUInteger i=0; i < coordCount; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; MKMapPoint p = MKMapPointForCoordinate(coords[i]);&nbsp; &nbsp; &nbsp; &nbsp; r = MKMapRectUnion(r, MKMapRectMake(p.x, p.y, 0, 0));&nbsp; &nbsp; }&nbsp; &nbsp; return MKCoordinateRegionForMapRect(r);}
随时随地看视频慕课网APP

相关分类

iOS
我要回答