Programmatically rotating a MKMapView in iOS7

2019-02-01 23:34发布

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.

1条回答
老娘就宠你
2楼-- · 2019-02-02 00:12

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

}
查看更多
登录 后发表回答