iosswiftmapkit

Tilt map in Mapkit programmatically using Swift


I'd like to tilt the map at startup. (Same as the user does when scrolling up or down with 2 fingers)

Is this possible using Swift?


Solution

  • MKMapView Class Reference : https://developer.apple.com/library/mac/documentation/MapKit/Reference/MKMapView_Class/index.html#//apple_ref/occ/instp/MKMapView/pitchEnabled

    Look at the camera property :

    A camera object defines a point above the map’s surface from which to view the map. Applying a camera to a map has the effect of giving the map a 3D-like appearance. You can use a camera to rotate the map so that it is oriented to match the user’s heading or to apply a pitch angle to tilt the plane of the map.

    Assigning a new camera to this property updates the map immediately and without animating the change. If you want to animate changes in camera position, use the setCamera:animated: method instead.

    You must not set this property to nil. To restore the map to a flat appearance, apply a camera with a pitch angle of 0, which yields a camera looking straight down onto the map surface.

    Try to create and set a camera (animated or not).

    Edit :

    I tried myself. Here is an example of how to use it :

    let userCoordinate = CLLocationCoordinate2D(latitude: 58.592725, longitude: 16.185962)
    let eyeCoordinate = CLLocationCoordinate2D(latitude: 58.571647, longitude: 16.234660)
    let mapCamera = MKMapCamera(lookingAtCenterCoordinate: userCoordinate, fromEyeCoordinate: eyeCoordinate, eyeAltitude: 400.0)
    
    let annotation = MKPointAnnotation()
    annotation.setCoordinate(userCoordinate)
    
    mapView.addAnnotation(annotation)
    
    mapView.setCamera(mapCamera, animated: true)
    

    You'll have to find your right eyeCoordinate depending on your location and tilt effect you want to have.