How to rotate MKMapView and keep the Annotation and the view don't rotate?

大城市里の小女人 提交于 2019-12-09 06:54:55

问题


I am making an MKMapView that shows the user's current location. I want to rotate the map like the Google Maps App without rotating the annotations. Im using the following code

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

but the whole view is rotated like this image. The image of the problem in my app


回答1:


You basically need to apply a the same transform to all annotation views but with negative angle, i.e.

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

If you're planning to add more annotations you should place the same code in mapView:didAddAnnotationViews: too:

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



回答2:


All the annotations and callouts that are shown on an MKMapView are subviews of MKMapView so if you rotate the container view all of its subviews are also going to be rotated.



来源:https://stackoverflow.com/questions/7036246/how-to-rotate-mkmapview-and-keep-the-annotation-and-the-view-dont-rotate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!