在地图上重叠注释(MKAnnotationView)的问题

Ale*_*ert 9 iphone objective-c mapkit mkmapview mkannotation

在我的iphone应用程序中,我使用MapKit和MKMapView以及自定义MKAnnotationView.

问题是当注释在地图上重叠时(在我的应用中,注释是照片而这些照片可能重叠),当你点击前面出现的注释时,它是另一个注释(在背面)接收事件(似乎是随机的) ).

我没有找到任何方法将事件发送到前端注释.我不敢相信这个bug /问题没有任何解决方案!

stackoverflow上的Z排序重叠注释问题的顺序对我没有多大帮助.

欢迎任何想法(即使是肮脏的解决方案)!

这是我的一些代码(没什么特别的,很常见):

CustomAnnotation.h

@interface CustomAnnotation : NSObject <MKAnnotation> {
   @private
   CustomAnnotationView* view;
}

    @property (nonatomic, retain) CustomAnnotationView* view;

@end
Run Code Online (Sandbox Code Playgroud)

CustomAnnotation.m

@implementation CustomAnnotation

@synthetize view;
Run Code Online (Sandbox Code Playgroud)

CustomAnnotationView.h

@interface CustomAnnotationView : MKAnnotationView {
}

@end
Run Code Online (Sandbox Code Playgroud)

CustomAnnotationView.m

@implementation CustomAnnotationView

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// Do something related to the annotation tapped
}

@end
Run Code Online (Sandbox Code Playgroud)

主类... //添加了注释,其中一些与其他注释重叠.

- (void)addAnnotation:(CustomAnnotation*)annotation {
    [map addAnnotation:annotation];
}

...

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

    NSString* identifier = getIdentifierFromAnnotation(annotation);
    CustomAnnotationView* view;
    if(!(view = (CustomAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:identifier])) {
        view = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: identifier];
        [(CustomAnnotation*)annotation setView:view];
        [view release];
    }
    return view;
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*ert 2

最后,我发现避免失去地图功能的最佳方法是:

  • 让 CustomAnnotationView (touchesEnded) 上的触摸侦听器(在每个注释上)

  • 当接收到触摸事件时,将事件传递给主类

  • 主类在注释上循环并保留具有良好位置(注释框架中的触摸事件)且位于前面的注释

它运作得很好。