MKCoordinateRegion可以稍微缩放吗?

5
我正在将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)似乎没有任何影响。

你的目标是自动缩放和平移以适应注释,还是手动操作? - Evan Mulawski
我正在将注释放在地图上,然后使用MKCoordinateRegion region = MKCoordinateRegionMake(locationCenter, locationSpan);来适应视图。我想要做的是轻微调整locationSpan,以获得略微(即5%)更紧凑的缩放效果。 - fuzzygoat
2个回答

6
较小的调整不会使地图视图缩放级别发生变化。当您传递一个区域时,地图视图决定使用什么缩放级别以获得最佳适应度。它永远不会使用“中间”级别,因为“中间”缩放级别看起来模糊。您提供要显示的区域,它会生成包括整个区域的缩放级别。将您想要的区域提供给regionThatFits:应该返回它使用的级别。
如果您用手指捏合来缩放地图,则可以到达两个缩放级别之间的级别,但如果您双击(缩小)或双指点击(放大)则只会看到“标准”缩放级别。
我在这里谈论缩放级别,但实际上它们在iOS中不存在与它们在Google Maps中存在的方式相同。只有区域存在,就像设置地图到某个级别一样。
对于您的问题,如何最好地适应标记,我发现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:返回的值并不总是地图视图最终设置的区域。这可能是一个bug,但从iOS 4.0开始就一直存在。你可以通过记录regionThatFits:返回的MKCoordinateRegion并将其与缩放后的地图视图区域进行比较来测试此问题。我记得在苹果开发者论坛上也有人提到过这个问题。


为了扩展nevan的回答,地图瓦片是以一系列不同分辨率预渲染的,这些分辨率必须具有离散的步骤,并且实际上通常具有相当大的步骤(存储便宜,但我们谈论的是整个星球 :))。 - Frank Schmitt
没错,我应该说的。我认为这些步骤只是将宽度减半(以使数学更容易)。因此,缩放512x512瓷砖使用4个512x512瓷砖来显示相同的区域。这篇帖子是我读过的最好的解释之一:http://troybrant.net/blog/2010/01/mkmapview-and-zoom-levels-a-visual-guide/ - nevan king
非常感谢,这很有道理。我提问的一半原因只是为了弄清楚“为什么”。将增量除以3.0对我来说缩放得非常远,我会试着玩一下看看能得出什么结果,但至少现在我走上了正确的轨道。 - fuzzygoat
没问题。奇怪的是需要不同的数字才能得到良好的拟合效果。我检查了我的代码,看是否有问题,但找不到导致这个问题的原因。 - nevan king

4
我发现这种方法非常有用。你只需要调用它并将你的 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];
}

你尝试过调整注释行上的1.1倍乘数吗?这应该会产生一个小的缩放效果。 - Evan Mulawski
我一直在更改的是1.1倍乘数。1.1会进行回撤,使区域变大,并在两侧提供更多空间。我想要的是推进/缩放,使区域变小,以便更紧密地填充屏幕。 - fuzzygoat
然后您可以将乘数更改为0.9,并根据自己的喜好进行调整。 - Evan Mulawski
这种行为是针对我发布的代码还是你一直在使用的代码? - Evan Mulawski
两位 @nevan 解释得很好,都能解释我所看到的。 - fuzzygoat
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接