如何旋转MKMapView并保持Annotation和视图不旋转?

Jia*_*vin 3 iphone annotations rotation mkmapview

我正在制作一个显示用户当前位置的MKMapView.我想像Google Maps App一样旋转地图而不旋转注释.我使用以下代码

-(void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
     mapView.transform=CGAffineTransformMakeRotation(-1*newHeading.magneticHeading*3.14159/180)};
Run Code Online (Sandbox Code Playgroud)

但整个视图像这个图像一样旋转. 我的应用程序中的问题的图像

小智 8

您基本上需要将相同的变换应用于所有注释视图,但具有负角度,即

for (id<MKAnnotation> annotation in mapView.annotations) 
{
    MKAnnotationView* annotationView = [mapView viewForAnnotation:annotation];
    [annotationView setTransform:CGAffineTransformMakeRotation(-angle)];
}
Run Code Online (Sandbox Code Playgroud)

如果您计划添加更多注释,您也应该放置相同的代码mapView:didAddAnnotationViews::

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    for (MKAnnotationView* annotationView in views) 
    {
        [annotationView setTransform:CGAffineTransformMakeRotation(-angle)];
    }
}
Run Code Online (Sandbox Code Playgroud)