稍微放大 MKCoordinateRegion?

发布于 2024-10-12 10:42:30 字数 985 浏览 3 评论 0原文

我正在缩放 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)似乎没有任何区别。

I am zooming an MKMapView to fit the bounding region of a collection of pins, however when the pins are displayed I have noticed that the zoom could ideally do with being a little tighter. My proposed solution to this was to make the region deltas slightly smaller:

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

However I have noticed that fine adjustments don't seem to translate to a small zoom increase, is there some form of snapping on the zoom? Really small values work, as do really big ones, but just adjusting the region size by a few percent does not seem to work with the view nearly always jumping/zooming in to far and clipping my pins.

EDIT:

Quick tests showing the results of different scaling factors on the region:

 //                                                             SCALE FACTOR
 //                                                                  V
 region.span.latitudeDelta  =   (upper.latitude - lower.latitude) * 0.9;
 region.span.longitudeDelta = (upper.longitude - lower.longitude) * 0.9;

Here are the results:

  • x0.5 region too small, some annotations off screen
  • x0.6 Same as using 1.0
  • x0.7 Same as using 1.0
  • x0.8 Same as using 1.0
  • x0.9 Same as using 1.0
  • x1.0 Original fit
  • x1.1 region too big, annotations too small on screen

My point is that very small adjustments (e.g. 0.6 to 0.9) don't seem to make any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Saygoodbye 2024-10-19 10:42:31

较小的调整永远不会使地图视图缩放级别发生变化。当您经过某个区域时,地图视图会决定使用哪种缩放级别来实现最佳拟合。它永远不会使用“中间”级别。原因是“中间”缩放级别看起来模糊。您给它您想要显示的区域,它会生成包含整个区域的缩放级别。将您想要的区域提供给 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: 中的 MKCooperativeRegion 并将其与缩放后的地图视图区域进行比较来自行测试。我似乎记得它出现在苹果开发者论坛上。

Smaller adjustments will never make the mapview zoom level change. When you pass a region, the mapview decides on what zoom level to use for the best fit. It will never use an "in-between" level. The reason is that the "in-between" zoom levels look fuzzy. You give it the region you want to show and it makes a zoom level that includes that whole region. Giving your desired region to regionThatFits: should return the level that it uses.

If you're zooming the map with a pinch, you can get to a level between two zoom levels, but if you double-tap (to zoom in) or do a 2-finger tap (to zoom out) you will only see the "standard" zoom levels.

I'm talking about zoom levels here, but really they don't exist in iOS in the same way they exist in Google Maps. Only regions exist as far as setting the map to a certain level.

With your problem of getting the best fit for pins, I found that something changed in iOS 4, and the code I'd used to fit pins suddenly gave too much space. I divided the deltas by 3 and it worked again. You might want to wrap this in a conditional to target only iOS 4.

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

Looking at your code, you use * 0.9 to get the exact same thing.

One of the strange things I found was that the value returned by regionThatFits: wasn't always the region that the mapview ended up setting. It might be a bug, but it's been there since iOS 4.0. You can test this yourself by logging the MKCoordinateRegion from regionThatFits: and comparing it to the mapview's region after zooming. I seem to remember it coming up on the Apple Developer Forums.

栀梦 2024-10-19 10:42:31

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

什么是缩小并适应 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];
}

I have found this method to be extremely useful. All you need to do is call it and pass your MKMapView as the argument, and it will determine the best zoom level to fit all of your annotations. The "tightness" can be adjusted by modifying the constant multiplier on the commented lines (currently 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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文