简体   繁体   中英

Programmatically rotating a MKMapView in iOS7

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.

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];
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM