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

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

问题描述

我熟悉使用Google Maps Javascript API。最近我开始为iPhone项目使用MapKit框架,但我很难在地图上找出缩放和设置区域。



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



当我使用MKMapView委托并跟踪regionDidChange代理方法结果似乎没有相互关联。当我缩小并看到span delta值在文档中指定的增加时,它是确定的。但突然,我拖动地图而不缩放,增量值变为0.0。



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



一个额外的问题是有什么方法可以指定一个MKMapView的最小最大缩放级别:)



感谢

解决方案

首先, MKMapView 不使用/



而是使用 MKCoordinateRegion 来描述MKMapView的可见区域,该区域由两个值组成:


  1. 中心(区域中心点)和



  2. 但是,span(这是一个 MKCoordinateSpan )包含:


    1. latitudeDelta (由区域代表的垂直距离)和

    2. longitudeDelta <


    3. 这是一个简单的例子。这是一个玩具MKCoordinate地区:


      1. center:


        • 纬度:0

        • 经度:0


      2. span:


        • latitudeDelta:8

        • longitudeDelta:6


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


      1. -hand point):


        • 纬度:-4

        • 经度:-3


      2. 最大坐标(右上角):


        • 纬度:4

        • 经度:3



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



      您可以使用此委托方法播放:

        mapView:regionWillChangeAnimated 

      例如通过将区域调整为允许的缩放级别。 (种类似桌子视图会让你滚过边缘,但橡胶带就会回到原位。)然而,你的里程可能会有所不同,因为我没有使用它为这个目的。



      btw,在OS 3.1中有明确的修正/改进对MapKit的方面,给我在3.0的麻烦。


      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

      解决方案

      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.

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

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