MKMapView 缩放和区域 [英] MKMapView Zoom and Region

查看:20
本文介绍了MKMapView 缩放和区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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).

当我使用 MKMapView 委托并跟踪 regionDidChange 委托方法中的跨度值时,结果似乎彼此不相关.当我缩小并看到跨度增量值按照文档中的指定增加时,没关系.但突然间我拖动地图而不缩放,增量值变为 0.0.

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.

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

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?

作为奖励问题,有没有办法在 MKMapView 上指定最小-最大缩放级别 :)

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

谢谢

推荐答案

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

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

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

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

  1. center(区域的中心点),以及
  2. span(围绕中心的可见区域的大小).
  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.)

然而,span(它是一个 MKCoordinateSpan)包括:

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

  1. latitudeDelta(区域代表的垂直距离),以及
  2. longitudeDelta(区域代表的水平距离).
  1. latitudeDelta (the vertical distance represented by the region), and
  2. longitudeDelta (the horizontal distance represented by the region).

一个简单的例子.这是一个玩具 MKCoordinateRegion:

A brief example. Here's a toy MKCoordinateRegion:

  1. 中心:
    • 纬度:0
    • 经度:0
  • 纬度Delta:8
  • longitudeDelta:6

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

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

  1. 最小坐标(左下点):
    • 纬度:-4
    • 经度:-3
  • 纬度:4
  • 经度:3

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

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.)

至于限制区域,你可以用这个delegate方法来玩:

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.

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

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

这篇关于MKMapView 缩放和区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆