MKMapView 缩放和区域
我熟悉使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,MKMapView 不像 Google 地图那样使用/具有一组预定义的缩放级别。
相反,MKMapView 的可见区域是使用 MKCooperativeRegion 来描述的,它由两个值组成:
中心点应该是明显的(它是区域的中心点。)
但是,跨度(它是一个 MKCooperativeSpan)由以下部分组成:
一个简短的例子。 这是一个玩具 MKCooperativeRegion:
该区域可以使用其最小和最大坐标来描述,如下所示:
因此,您可以使用适当大小的 MKCooperativeSpan 来指定中心点周围的缩放级别。 作为 Google 数字缩放级别的近似值,您可以对 Google 用于给定缩放级别的跨度大小进行逆向工程,并相应地创建一个跨度。 (Google 以与 MKMapView 相同的方式描述其视图区域,即中心 + 跨度,因此您可以将这些值从 Google 地图中提取出来。)
至于限制区域,您可以使用此委托方法:
例如通过调整大小将区域恢复到允许的缩放级别。 (有点像表格视图让您滚动越过边缘,但随后橡皮筋又会回到原位。)但是,您的里程可能会有所不同,因为我没有将其用于此目的。
顺便说一句,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:
The center point should be obvious (it's the center point of the region.)
However, span (which is a MKCoordinateSpan) consists of:
A brief example. Here's a toy MKCoordinateRegion:
The region could be described using its min and max coordinates as follows:
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:
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.
如果您更喜欢使用显式缩放级别而不是定义 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.
跨度以纬度和经度为单位。 有一种方法可以用来构造需要距离的 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.
使用maps.google.com 通过检查链接查询字符串来快速比较某个位置的缩放级别,结果显示 dx 和 dy 跨度值增加了 2 倍:
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:
Brant 在 MKMapView 上的类别效果很好。 但是,在计算 mapSizeInPixels 时,它似乎尚未更新以支持具有视网膜屏幕的较新设备。
可以通过替换此行来修复它:
使用此行:
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:
With this line: