0

Objective

I need my app to zoom in or out a MKMapView.

Code

I thought of animating the value of the altitude of a MKMapCamera. Though, this doesn't zoom in or out the map.

let mapCamera = MKMapCamera()
    mapCamera.altitude = 1000000
    mapView.camera = mapCamera

    UIView.animateWithDuration(10, delay: 0., options: UIViewAnimationOptions.CurveLinear, animations: {
        mapCamera.altitude = 6000000
        }, completion: {(finished: Bool) in
    })

    mapView.camera = mapCamera

Question

How do I zoom in or out a MKMapView? Are there any other ways to do so?

  • 1
    possible duplicate of [Zoom in a MKMapView programmatically](http://stackoverflow.com/questions/1031787/zoom-in-a-mkmapview-programmatically) – Aaron Brager Apr 19 '15 at 15:35
  • Thanks. There is no Swift code in these answers. Though, I am trying to convert it right now. –  Apr 19 '15 at 16:00

2 Answers2

3

Here is an extension based on kevins answer https://stackoverflow.com/a/20129379/1488696

With it you'll be able to zoom in and out as required

extension MKMapView {

    // delta is the zoom factor
    // 2 will zoom out x2
    // .5 will zoom in by x2

    func setZoomByDelta(delta: Double, animated: Bool) {
        var _region = region;
        var _span = region.span;
        _span.latitudeDelta *= delta;
        _span.longitudeDelta *= delta;
        _region.span = _span;

        setRegion(_region, animated: animated)
    }
}

Use it like so: myMapView.setZoomByDelta(0.5, animated: true)

MKMapCamera is used for 3D maps. So its likely not what you're looking for

Apple Docs

An MKMapCamera object describes a virtual camera that you use to define the appearance of the map. A camera object creates a virtual viewpoint above the map surface and affects how the map renders its tiles and other content. You use a camera object to specify the location of the camera on the map, the compass heading that corresponds to the camera’s viewing direction, the pitch of the camera relative to the map perpendicular, and the camera’s altitude above the map. These factors let you create a map view that is not just flat but offers a more 3D-like experience."

Community
  • 1
  • 1
0

Use the method setRegion:animated:. It takes an MKCoordinateRegion parameter. The region includes a span, which determines the range of north-to-south latitude shown on the screen.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks for answering my question! Does the `setRegion:animated: `method define the altitude? Please note I didn't downvote. – Cesare Apr 19 '15 at 15:39
  • I haven't messed with altitude before. That was added to the map kit later and I haven't had occasion to use it yet. – Duncan C Apr 19 '15 at 16:06