【问题标题】:Slight zoom on MKCoordinateRegion?稍微放大 MKCoordinateRegion?
【发布时间】:2011-01-18 12:44:01
【问题描述】:

我正在缩放 MKMapView 以适合一组引脚的边界区域,但是当引脚显示时,我注意到缩放可以理想地做得更紧一点。我对此提出的解决方案是使区域增量略小:

// SMALL ZOOM
region.span.latitudeDelta = (upper.latitude - lower.latitude) * 0.9;
region.span.longitudeDelta = (upper.longitude - lower.longitude) * 0.9;

但是我注意到微调似乎不会转化为小的缩放增加,是否存在某种形式的缩放捕捉?非常小的值和非常大的值一样有效,但仅将区域大小调整几个百分点似乎并不适用于几乎总是跳/放大到远处并剪裁我的图钉的视图。

编辑:

显示区域上不同比例因子结果的快速测试:

 //                                                             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 相同
  • x0.9 与使用 1.0 相同
  • x1.0 原版合身
  • x1.1 区域太大,屏幕上的注释太小

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

【问题讨论】:

  • 您的目标是自动缩放和平移以适应您的注释还是手动执行?
  • 我将注释放在地图上,然后使用 MKCoordinateRegion region = MKCoordinateRegionMake(locationCenter, locationSpan);然后适合视图。我真正要做的是稍微调整 locationSpan 以获得稍微(即 5%)更紧密的缩放。

标签: iphone objective-c cocoa-touch mapkit mkmapview


【解决方案1】:

较小的调整永远不会改变地图视图的缩放级别。当您通过一个区域时,地图视图会决定使用哪种缩放级别最适合。它永远不会使用“中间”级别。原因是“中间”缩放级别看起来很模糊。你给它你想要显示的区域,它会产生一个包含整个区域的缩放级别。将所需区域提供给 regionThatFits: 应该会返回它使用的级别。

如果您通过捏合来缩放地图,则可以达到两个缩放级别之间的级别,但如果您双击(放大)或两指点击(缩小),您将只能看到“标准”缩放级别。

我在这里谈论的是缩放级别,但实际上它们在 iOS 中的存在方式与在 Google 地图中的存在方式不同。只要将地图设置为某个级别,就只存在区域。

由于您遇到的最适合大头针的问题,我发现 iOS 4 中发生了一些变化,而我用来安装大头针的代码突然提供了太多空间。我将增量除以 3,它再次起作用。您可能希望将其包装在仅针对 iOS 4 的条件中。

region.span.longitudeDelta = (maxCoord.longitude - minCoord.longitude) / 3.0;
region.span.latitudeDelta = (maxCoord.latitude - minCoord.latitude) / 3.0;

查看您的代码,您使用* 0.9 得到完全相同的东西。

我发现的一件奇怪的事情是regionThatFits: 返回的值并不总是地图视图最终设置的区域。这可能是一个错误,但它自 iOS 4.0 以来就一直存在。您可以通过从regionThatFits: 记录MKCoordinateRegion 并在缩放后将其与地图视图的区域进行比较来自行测试。我似乎记得它出现在 Apple 开发者论坛上。

【讨论】:

  • 为了扩展 nevan 的答案,地图图块以一系列不同的分辨率进行预渲染,这些分辨率必然具有离散的步骤,并且实际上通常具有相当大的步骤(存储很便宜,但这是我们正在谈论的整个星球 :)
  • 是真的,我应该这么说的。我认为这些步骤都只是宽度的一半(使数学更容易)。因此,放大 512x512 瓦片使用 4 个 512x512 瓦片来显示同一区域。这篇文章有我读过的最好的解释之一:troybrant.net/blog/2010/01/…
  • 非常感谢,这很有意义。我问的一半原因只是为了找出“为什么”。将增量除以 3.0 对我来说放大得很远,我会玩一玩,看看我能想出什么,但至少现在我走在了正确的轨道上。
  • 没问题。奇怪的是,它采用不同的数字来获得合适的身材。我查看了我的代码以查看是否有任何问题,但找不到导致此问题的原因。
【解决方案2】:

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

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

- (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];
}

【讨论】:

  • 嗨,埃文,这就是我在自己的日常生活中一直在做的事情。我确实错过了“regionThatFits”,但我刚刚添加了它,它似乎没有任何区别。我的问题是,使用像 x0.6、x0.7、x0.8、x0.9 这样的小乘数(收紧区域)都应该围绕其中心缩小区域都会产生相同的结果。 x1.0 太远了,x0.9 太远了,这似乎不是一个快乐的媒介。
  • 您是否尝试过调整注释行上的 1.1 倍乘数?这应该会产生一个小的缩放效果。
  • 1.1x 乘数是我一直在改变的。 1.1正在做一个回调,使区域更大,并在两侧提供更多空间。我想要的是推入/缩放,使区域更小,使区域更紧密地填充屏幕。
  • 然后您将乘数更改为 0.9,并根据自己的喜好进行调整。
  • 虽然可以,但不能根据自己的喜好调整!设置 0.9 看起来与设置 1.0 相同,甚至设置 0.6 看起来与 1.0 相同。只有当您达到 0.5 时,视图才会移近,此时距离太近了。
猜你喜欢
  • 2014-04-15
  • 2018-06-30
  • 1970-01-01
  • 2020-02-28
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 2018-01-08
  • 2014-05-14
相关资源
最近更新 更多