简体   繁体   English

iOS旋转MKAnnotationView以响应MKMapView旋转

[英]iOS rotating MKAnnotationView in response of MKMapView rotation

In my application I have a MKMapView where several annotations are shown. 在我的应用程序中,我有一个MKMapView,其中显示了几个注释。 The map rotates based on the heading of the device. 地图根据设备的方向旋转。 To rotate the map the following statement is performed (called by the method locationManager: didUpdateHeading:) 要旋转地图,执行以下语句(由locationManager方法调用:didUpdateHeading :)

self.navigationMapView.mapView.transform = CGAffineTransformMakeRotation(-heading);

where the heading (magnetic) is expressed in radians. 方向(磁性)以弧度表示。 What I noticed it's that even the annotations in the map rotate and I don't want it. 我注意到的是,即使地图中的注释也旋转了,但我不希望这样做。 I tried to fix it in the following method: 我尝试通过以下方法修复它:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

    static NSString *identifier = @"AnnotationViewIdentifier";

    MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (av == nil) {
        av = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
    }
    else{
        av.annotation = annotation;
    }

    av.transform = CGAffineTransformMakeRotation(degreesToRadians(self.arController.currentHeading.magneticHeading)); 

    av.canShowCallout = YES;
    return av;

} 

and I want to call this method from "didUpdateHeading:" but I really don't know how to do it. 我想从“ didUpdateHeading:”中调用此方法,但我真的不知道该怎么做。 The TableView class has the reloadData function that calls the delegate method but here the things seem different. TableView类具有reloadData函数,该函数调用委托方法,但此处的情况似乎有所不同。 Any suggestions?! 有什么建议么?!

Another question, my annotations on the map show the distance from the user, I would like to update them (distance label) as soon as the user change location. 另一个问题是,我在地图上的注释显示了距用户的距离,我想在用户更改位置后立即更新它们(距离标签)。 Any suggestions?! 有什么建议么?!

So with a MKMapView having that be called properly is a little bit annoying. 因此,使用MKMapView正确地调用它会有些烦人。 Essentially you have one of two options. 本质上,您有两种选择之一。 Option 1: Create an array of the annotation on the screen and remove that from the map_view and then re-add them to the map_view. 选项1:在屏幕上创建注释数组,然后将其从map_view中删除,然后将其重新添加到map_view中。 Essentially creating your own reload data function. 本质上是创建自己的重载数据功能。 Option 2: Do something simple such as 选项2:做一些简单的事情,例如

CGLocationCoordinate2D coordinate = map_view.center;
 map_view.center = coordinate;

-- Essentially the point is to reset a property of the map causing it to redraw. -本质上来说,重点是重置地图的属性以使其重绘。 However this option is not always going to work. 但是,此选项并非总是可行。 Option 1 has a higher chance of working however that one can also fail, so if simply taking the annotations off and re-adding them causes nothing to happen then simply decreate the map and then recreate the map at the end of your map refresh function something like. 选项1的工作机会更高,但是也可能失败,因此,如果简单地删除注释并重新添加注释,则什么也没有发生,那么只需取消创建地图,然后在地图刷新功能结束时重新创建地图,喜欢。

[my_map_view removeFromSuperView];
[my_map_view release];
my_map_view = nil;
my_map_view = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,320,480)];

one of these options should work. 这些选项之一应该起作用。 I had to do option one for my solution however I know some people are lucky and option 2 works just as well. 我必须为解决方案选择选项一,但是我知道有些人很幸运,选项二也能正常工作。

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

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