【问题标题】:Update region to be around Annotations将区域更新为注释周围
【发布时间】:2013-10-13 11:28:22
【问题描述】:

我有一个带有一些注释的mapView,我想将地图围绕annotations 居中。我有以下代码:

- (void)updateRegion {
    self.needUpdateRegion = NO;
    CGRect boundingRect;
    BOOL started = NO;
    for (id <MKAnnotation> annotation in self.mapView.annotations){
        CGRect annotationRect = CGRectMake(annotation.coordinate.longitude, annotation.coordinate.latitude, 0, 0);
        if (!started) {
            started = YES;
            boundingRect = annotationRect;
        } else {
            boundingRect = CGRectUnion(boundingRect, annotationRect);
        }
    } if (started) {
        boundingRect = CGRectInset(boundingRect, -0.2, -0.2);
        if ((boundingRect.size.width >20) && (boundingRect.size.height >20)) {
            MKCoordinateRegion region;
            region.center.latitude = boundingRect.origin.x + boundingRect.size.width /2;
            region.center.longitude = boundingRect.origin.y + boundingRect.size.height / 2;
            region.span.latitudeDelta = boundingRect.size.width;
            region.span.longitudeDelta = boundingRect.size.height;
            [self.mapView setRegion:region animated:YES];

        }
    }
}

它在viewDidAppear 中执行以产生“滑动效果”:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.needUpdateRegion) [self updateRegion];
}

当我运行应用程序时,它什么也不做,只显示美国。 显示注释(在欧洲)。

【问题讨论】:

    标签: ios ios7 mkmapview mkannotation


    【解决方案1】:

    假设首先调用updateRegion(确保needUpdateRegion被初始化为YES),updateRegion方法有两个主要问题:

    1. 如果生成的边界图 rect 的宽度和高度为 20,它只会调用 setRegion。由于您使用纬度和经度度数进行计算,这意味着 setRegion 只会在生成的边界图被调用 rect 超过 20 纬度/经度宽/高。不清楚这是否是您的意图。
    2. region 属性正在向后设置。在边界图rect的计算中,x的值被设置为经度,y的值被设置为纬度。但是在设置region.center.latitude 时,它使用的是boundingRect.origin.x 而不是boundingRect.origin.y。这也适用于其他属性,因此代码应该是:

      region.center.longitude = boundingRect.origin.x + boundingRect.size.width /2;
      region.center.latitude = boundingRect.origin.y + boundingRect.size.height / 2;
      region.span.longitudeDelta = boundingRect.size.width;
      region.span.latitudeDelta = boundingRect.size.height;
      


    请注意,iOS 7 提供了一种新的便捷方法showAnnotations:animated: 来自动显示注释,因此您不必自己计算区域。

    所以在updateRegion 中,您可以执行以下操作:

    - (void)updateRegion {
        self.needUpdateRegion = NO;
    
        //if showAnnotations:animated: is available, use it...
        if ([mapView respondsToSelector:@selector(showAnnotations:animated:)])
        {
            [self.mapView showAnnotations:mapView.annotations animated:YES];
            return;
        }
    
        //calculate region manually...
    }
    

    【讨论】:

    • 感谢您的回答。 showAnnotations:animated: 运行良好。我还可以通过将数字交换为非常小的数字来修复我的原始代码(因为注释彼此非常接近)。由于我是专门为 iOS 7 编写的应用程序,是否最好放弃原始代码而只使用showAnnotations:animated:
    • 如果该应用仅适用于 iOS 7 或更高版本,那么是的,如果适合您,我会使用 showAnnotations。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多