简体   繁体   English

以编程方式在iOS7中旋转MKMapView

[英]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. 我有一个应用程序,当前使用CGAffineTransformMakeRotation来操作MKMapView,以显示具有正确方向和大小的地图。 With the release of iOS7 this method has become unreliable (map center keeps shifting). 随着iOS7的发布,这种方法变得不可靠(地图中心不断变换)。 I am hoping to solve this with a more reliable solution. 我希望通过更可靠的解决方案来解决这个问题。

Is there a way to rotate the map in code without using CGAffineTransformMakeRotation? 有没有办法在不使用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. 我看了MKMapCamera,希望我可以操纵它来传递staic值来操纵地图但是没有办法手动设置centerCoordinate和eyeCoordinate。

You can rotate and pitch the map by setting a new MKMapCamera with -setCamera:animated: . 您可以通过使用-setCamera:animated:设置新的MKMapCamera来旋转和调整地图。

To set the rotation, give it a new heading parameter: 要设置旋转,请为其指定一个新的heading参数:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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