MKMapView 缩放和区域

发布于 2024-07-29 05:35:05 字数 537 浏览 1 评论 0原文

我熟悉使用 Google Maps Javascript API。 最近我开始在 iphone 项目中使用 MapKit 框架,但我很难弄清楚缩放和在地图上设置区域。

在 Google Maps API 中,我曾经使用整数缩放级别(如 8、9、10)以及简单的函数 setZoom()。 我在 MapKit 框架中看到的唯一等效方法是 setRegion:animated。 据我了解,我需要设置区域跨度的纬度和经度“增量”值来指定缩放级别。 但我真的不知道这些值代表什么(我阅读了文档)。

当我使用 MKMapView 委托并跟踪 RegionDidChange 委托方法结果中的跨度值时,结果似乎彼此不相关。 当我缩小并看到跨度增量值按照文档中指定的方式增加时,这是可以的。 但突然我拖动地图而不缩放,增量值变成 0.0。

有人可以解释一下这些跨度和增量的参考点是什么吗? 或者是否有任何算法可以将整数缩放级别(如 9)转换为这些增量值?

作为一个额外的问题,有什么方法可以在 MKMapView 上指定最小-最大缩放级别:)

谢谢

I'm familiar with using Google Maps Javascript API. Recently I started using MapKit framework for an iphone project, but I'm having a hard time to figure out zooming and setting a region on map.

In Google Maps API I used to use integer zoom levels like 8, 9, 10 along with straightforward function setZoom(). The only equivalent method I can see in the MapKit framework is setRegion:animated. As I understand, I need to set a region's span's latitude and longitude "delta" values to specify zoom level. But I really don't have an idea what these values represent(I read the documentation).

When I use a MKMapView delegate and trace the span values in regionDidChange delegate method results don't seem to correlate each other. It's ok when I zoom out and see the span delta values are increasing as specified in documentation. But suddenly I drag the map without zooming and delta values become 0.0.

Can somebody please explain what is the reference point to these span and delta? Or is there any algorithm to convert an integer zoom level(like 9) to these delta values?

As a bonus question is there any way to specify a minimum-maximum zoom level on a MKMapView :)

Thanks

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

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

发布评论

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

评论(5

原野 2024-08-05 05:35:05

首先,MKMapView 不像 Google 地图那样使用/具有一组预定义的缩放级别。

相反,MKMapView 的可见区域是使用 MKCooperativeRegion 来描述的,它由两个值组成:

  1. center(区域的中心点)和
  2. span strong>(中心周围可见区域的大小)。

中心点应该是明显的(它是区域的中心点。)

但是,跨度(它是一个 MKCooperativeSpan)由以下部分组成:

  1. latitudeDelta(由区域)和
  2. longitudeDelta(区域代表的水平距离)。

一个简短的例子。 这是一个玩具 MKCooperativeRegion:

  1. center:
    • 纬度:0
    • 经度:0
  2. 跨度:
    • 纬度Delta:8
    • 经度增量:6

该区域可以使用其最小和最大坐标来描述,如下所示:

  1. 最小坐标(左下点):
    • 纬度:-4
    • 经度:-3
  2. 最大坐标(右上角点):
    • 纬度:4
    • 经度:3

因此,您可以使用适当大小的 MKCooperativeSpan 来指定中心点周围的缩放级别。 作为 Google 数字缩放级别的近似值,您可以对 Google 用于给定缩放级别的跨度大小进行逆向工程,并相应地创建一个跨度。 (Google 以与 MKMapView 相同的方式描述其视图区域,即中心 + 跨度,因此您可以将这些值从 Google 地图中提取出来。)

至于限制区域,您可以使用此委托方法:

mapView:regionWillChangeAnimated

例如通过调整大小将区域恢复到允许的缩放级别。 (有点像表格视图让您滚动越过边缘,但随后橡皮筋又会回到原位。)但是,您的里程可能会有所不同,因为我没有将其用于此目的。

顺便说一句,OS 3.1 对 MapKit 的某些方面进行了明确的修复/改进,这些修复/改进在 3.0 中给我带来了麻烦。

First of all, MKMapView does not use/have a predefined set of zoom levels like Google Maps does.

Instead, the visible area of a MKMapView is described using MKCoordinateRegion, which consists of two values:

  1. center (the center point of the region), and
  2. span (the size of the visible area around center).

The center point should be obvious (it's the center point of the region.)

However, span (which is a MKCoordinateSpan) consists of:

  1. latitudeDelta (the vertical distance represented by the region), and
  2. longitudeDelta (the horizontal distance represented by the region).

A brief example. Here's a toy MKCoordinateRegion:

  1. center:
    • latitude: 0
    • longitude: 0
  2. span:
    • latitudeDelta: 8
    • longitudeDelta: 6

The region could be described using its min and max coordinates as follows:

  1. min coordinate (lower left-hand point):
    • latitude: -4
    • longitude: -3
  2. max coordinate (upper right-hand point):
    • latitude: 4
    • longitude: 3

So, you can specify zoom levels around a center point by using an appropriately sized MKCoordinateSpan. As an approximation of Google's numeric zoom levels, you could reverse engineer the span sizes that Google uses for a given zoom level and create a span, accordingly. (Google describes their view regions in the same way that MKMapView does, as a center + span, so you can pull these values out of Google Maps.)

As for restricting the region, you may play w/ this delegate method:

mapView:regionWillChangeAnimated

e.g. by resizing the region back into your allowed zoom levels. (Kind of like how table views will let you scroll past the edge, but will then rubber band back into place.) However, your mileage may vary, since I haven't used it for this purpose.

btw, there are definite fixes/improvements in OS 3.1 to aspects of MapKit that were giving me trouble in 3.0.

失眠症患者 2024-08-05 05:35:05

如果您更喜欢使用显式缩放级别而不是定义 MKCooperativeSpan,我编写了一个类别,添加了对指定 MKMapView 缩放级别的支持。 代码可以在此处。

If you prefer using explicit zoom levels instead of defining an MKCoordinateSpan, I wrote a category that adds support for specifying the zoom level of an MKMapView. The code can be found here.

妳是的陽光 2024-08-05 05:35:05

跨度以纬度和经度为单位。 有一种方法可以用来构造需要距离的 MKCooperativeRegion 结构。 您可能正在使用 MKCooperativeRegionMakeWithDistance 来指定跨度,然后当您在 RegionDidChange 中检查它时,您会看到纬度/经度跨度,这就是它在 MKCooperativeRegion 结构中的存储方式。

据我所知,在使用 MKMapKit 时,整数缩放级别根本不可用或无用。 我个人更喜欢使用跨度数字,它更灵活。

你无法指定最大和最小缩放,而且我不知道如何破解它。MKMapKit 现在实际上相当弱,我对缺乏功能感到非常失望。

The span is in degrees of latitude and longitude. There is a method for constructing MKCoordinateRegion structs that takes distance, instead. It may be that you are using MKCoordinateRegionMakeWithDistance to specify the span, and then when you check it in regionDidChange, you're seeing at the lat/long span, which is how it is stored in an MKCoordinateRegion struct.

As far as I know, the integer zoom levels are not available or useful at all when working with MKMapKit. I personally prefer using the span figures, its more flexible.

You cannot specify max and min zoom, and I don't know of a way to hack it in. MKMapKit is actually pretty weak right now, I'm pretty disappointed by the lack of features.

左耳近心 2024-08-05 05:35:05

使用maps.google.com 通过检查链接查询字符串来快速比较某个位置的缩放级别,结果显示 dx 和 dy 跨度值增加了 2 倍:

 (0.005334, 0.011834) starting span
 (0.010668, 0.023668) dx: x2, dy: x2 
 (0.021335, 0.047337) dx: x2, dy: x2
 (0.042671, 0.094671) dx: x2, dy: x2
  ...

A quick comparison of zoom levels for a location using maps.google.com by inspecting the link querystring shows that the dx and dy span values increase by a factor of 2:

 (0.005334, 0.011834) starting span
 (0.010668, 0.023668) dx: x2, dy: x2 
 (0.021335, 0.047337) dx: x2, dy: x2
 (0.042671, 0.094671) dx: x2, dy: x2
  ...
早茶月光 2024-08-05 05:35:05

Brant 在 MKMapView 上的类别效果很好。 但是,在计算 mapSizeInPixels 时,它似乎尚未更新以支持具有视网膜屏幕的较新设备。

可以通过替换此行来修复它:

CGSize mapSizeInPixels = mapView.bounds.size;

使用此行:

CGSize mapSizeInPixels = CGSizeMake(mapView.bounds.size.width * [UIScreen mainScreen].scale, mapView.bounds.size.height * [UIScreen mainScreen].scale);

Brant's category on MKMapView works well. However, it appears that it has not been updated to support newer devices with retina screens when calculating mapSizeInPixels.

It can be fixed by replacing this line:

CGSize mapSizeInPixels = mapView.bounds.size;

With this line:

CGSize mapSizeInPixels = CGSizeMake(mapView.bounds.size.width * [UIScreen mainScreen].scale, mapView.bounds.size.height * [UIScreen mainScreen].scale);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文