首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >稍微放大MKCoordinateRegion?

稍微放大MKCoordinateRegion?
EN

Stack Overflow用户
提问于 2011-01-18 12:44:01
回答 2查看 4.6K关注 0票数 5

我正在缩放一个MKMapView,以适应一组引脚的边界区域,但是当显示引脚时,我已经注意到,理想的情况下,缩放可以更紧一点。我建议的解决办法是使该区域的三角洲稍微缩小一点:

代码语言:javascript
复制
// SMALL ZOOM
region.span.latitudeDelta = (upper.latitude - lower.latitude) * 0.9;
region.span.longitudeDelta = (upper.longitude - lower.longitude) * 0.9;

然而,我注意到,细微的调整似乎不能转化为一个小的放大,有没有某种形式的抓拍变焦?非常小的值起作用,就像非常大的值一样,但是仅仅将区域大小调整几个百分点似乎并不适用于视图,几乎总是跳进/放大到很远的地方,夹住我的引脚。

编辑:

快速测试显示了该区域不同尺度因素的结果:

代码语言:javascript
复制
 //                                                             SCALE FACTOR
 //                                                                  V
 region.span.latitudeDelta  =   (upper.latitude - lower.latitude) * 0.9;
 region.span.longitudeDelta = (upper.longitude - lower.longitude) * 0.9;

以下是研究结果:

  • x0.5区域太小,一些非屏幕注释
  • x0.6与使用1.0
  • x0.7相同,与使用1.0
  • x0.8相同,与使用1.0

H 111x0.9相同,与使用1.0<代码>H 212<代码>H 113x1.0原始适配<代码>H 214<代码>H 115X1.1区域太大,屏幕上注释太小<>H 216F 217

我的观点是,非常小的调整(例如0.6到0.9)似乎没有任何区别。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-25 23:32:32

较小的调整永远不会使地图视图缩放级别发生变化。当您通过一个区域时,地图视图将决定使用哪个缩放级别来实现最佳匹配。它永远不会使用“中间”级别。原因是“中间”缩放级别看上去模糊。你给它你想要显示的区域,它会产生一个缩放级别,包括整个区域。将所需区域交给regionThatFits:,应该返回它使用的级别。

如果你用夹子放大地图,你可以在两个缩放级别之间达到一个级别,但是如果你双击(放大)或者做两个手指点击(放大),你只会看到“标准”缩放级别。

我在这里说的是缩放级别,但实际上,在iOS中,它们并不像在Google中那样存在。只有区域存在,直到将地图设置到一定的水平。

由于您的问题是如何使引脚最合适,我发现iOS 4中有些地方发生了变化,我用来安装引脚的代码突然给出了太多的空间。我把三角洲除以3,它又起作用了。您可能需要将其包装在一个条件中,以便只针对iOS 4。

代码语言:javascript
复制
region.span.longitudeDelta = (maxCoord.longitude - minCoord.longitude) / 3.0;
region.span.latitudeDelta = (maxCoord.latitude - minCoord.latitude) / 3.0;

查看您的代码,您可以使用* 0.9获取完全相同的内容。

我发现的一件奇怪的事情是,regionThatFits:返回的值并不总是映射视图设置的区域。这可能是个bug,但从iOS 4.0开始就已经存在了。您可以通过从MKCoordinateRegion中记录regionThatFits:并在缩放后将其与mapview的区域进行比较来测试这一点。我似乎还记得苹果开发者论坛上提到的这件事。

票数 6
EN

Stack Overflow用户

发布于 2011-01-18 18:21:37

我发现这种方法非常有用。您所需要做的就是调用它并传递您的MKMapView作为参数,它将确定适合所有注释的最佳缩放级别。“紧度”可以通过修改注释行上的常量乘数来调整(目前为1.1)。

What's the best way to zoom out and fit all annotations in MapKit

代码语言:javascript
复制
- (void)zoomToFitMapAnnotations:(MKMapView *)mapView
{
    if ([mapView.annotations count] == 0)
        return;

    CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

    CLLocationCoordinate2D bottomRightCoord;
    bottomRightCoord.latitude = 90;
    bottomRightCoord.longitude = -180;

    for (MapAnnotation *annotation in mapView.annotations)
    {
        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

        bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
        bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
    }

    MKCoordinateRegion region;
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
    region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides

    region = [mapView regionThatFits:region];
    [mapView setRegion:region animated:YES];
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4724176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档