Programmatically rotating a MKMapView in iOS7

Andrin picture Andrin · Sep 21, 2013 · Viewed 13.7k times · Source

I have an app that currently uses CGAffineTransformMakeRotation to manipulate a MKMapView in order to display the map with the correct orientation and size. With the release of iOS7 this method has become unreliable (map center keeps shifting). I am hoping to solve this with a more reliable solution.

Is there a way to rotate the map in code without using CGAffineTransformMakeRotation?

I looked at the MKMapCamera in hopes I could manipulate it into passing staic values to manipulate the map but there is no way to manually set the centerCoordinate and the eyeCoordinate.

Answer

nevan king picture nevan king · Sep 23, 2013

You can rotate and pitch the map by setting a new MKMapCamera with -setCamera:animated:.

To set the rotation, give it a new heading parameter:

- (void)viewDidAppear:(BOOL)animated // or wherever works for you
{
    [super viewDidAppear:animated];

    if ([mapView respondsToSelector:@selector(camera)]) {
        MKMapCamera *newCamera = [[mapView camera] copy];
        [newCamera setHeading:90.0]; // or newCamera.heading + 90.0 % 360.0
        [mapView setCamera:newCamera animated:YES];
    }
}

You can also do a more fancy zoom with pitch and altitude change, showing buildings:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if ([mapView respondsToSelector:@selector(camera)]) {
        [mapView setShowsBuildings:YES];
        MKMapCamera *newCamera = [[mapView camera] copy];
        [newCamera setPitch:45.0];
        [newCamera setHeading:90.0];
        [newCamera setAltitude:500.0];
        [mapView setCamera:newCamera animated:YES];
    }

}