Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView Zoom and Region

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

like image 466
cocoatoucher Avatar asked Jul 22 '09 16:07

cocoatoucher


People also ask

How do you get zoom level in MKMapView?

The easiest way to get an Integer of the current zoom level, is by using the MapView function: regionDidChangeAnimated. This function recognizes every change in zoom and will give you the basis for the calculation of the zoom factor.

How do I zoom in on mapView?

MKCoordinateRegion zoomIn = mapView. region; zoomIn. span. latitudeDelta *= 0.5; [mapView setRegion:zoomIn animated:YES];

What is span in map view?

The horizontal and vertical span representing the amount of map to display.

How do you zoom map in react native?

It takes a region object which has latitudeDelta and longitudeDelta . Use these to set the zoom level. Updated: in a Region object the latitude and longitude specify the center location and latitudeDelta and longitudeDelta specify the span of the viewable map area.


3 Answers

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.

like image 180
Kelvin Avatar answered Oct 17 '22 17:10

Kelvin


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.

like image 36
Troy Brant Avatar answered Oct 17 '22 17:10

Troy Brant


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.

like image 4
Alice Isabelle Avatar answered Oct 17 '22 18:10

Alice Isabelle