Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tilt map in Mapkit programmatically using Swift

Tags:

ios

swift

mapkit

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?

like image 409
Christian Avatar asked Dec 11 '22 00:12

Christian


1 Answers

MKMapView Class Reference : http://goo.gl/djHXPn

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.

like image 84
lchamp Avatar answered Dec 28 '22 04:12

lchamp