MKMapView Zoom and Region
Asked Answered
H

5

87

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

Hadhramaut answered 22/7, 2009 at 16:16 Comment(1)
Simple modern solution is right here: https://mcmap.net/q/243196/-how-to-find-current-zoom-level-of-mkmapviewGrahamgrahame
E
136

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.

Elkins answered 22/7, 2009 at 19:36 Comment(2)
@Kelvin, can you describe more detail how to "reverse engineer the span sizes that Google uses for a given zoom level and create a span". I really need itLudwigshafen
This is incredibly out of date! Simple modern one word answer: https://mcmap.net/q/243196/-how-to-find-current-zoom-level-of-mkmapviewGrahamgrahame
T
118

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.

Trisa answered 23/1, 2010 at 2:49 Comment(2)
Troy's blog posts gave me a great understanding of how mapkit and google maps work. I highly recommend reading it.Yeti
git://github.com/jdp-global/MKMapViewZoom.gitMarigraph
P
4

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.

Polyzoic answered 22/7, 2009 at 18:9 Comment(0)
R
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:

 (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
  ...
Repatriate answered 11/12, 2009 at 0:30 Comment(0)
O
1

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);
Oribelle answered 19/6, 2014 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.