为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

iOS
慕虎7371278 2020-02-03 15:16:46
我有几个要添加到我的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 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];}请注意,这一切都是在我收到位置更新信息时发生的...我不知道这是否合适。如果没有,哪里会有更好的地方?-viewDidLoad?提前致谢。
查看完整描述

4 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

从iOS7开始,您可以使用showAnnotations:animated:


[mapView showAnnotations:annotations animated:YES];


查看完整回答
反对 回复 2020-02-03
?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

为什么这么复杂?


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);

}


查看完整回答
反对 回复 2020-02-03
  • 4 回答
  • 0 关注
  • 831 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信