在我的 iPhone 应用程序中,我将 MapKit 与 MKMapView 和自定义 MKAnnotationView 结合使用。
问题是当注释在地图上重叠时(在我的应用程序中,注释是照片,这些照片可能会重叠),并且当您点击前面出现的注释时,另一个注释(在背面)会接收事件(似乎是随机的) )。
我没有找到任何方法将事件发送到前面的注释。
我不敢相信这个错误/问题没有任何解决方案!
Z排序 https://stackoverflow.com/questions/1681565/z-ordering-of-mkannotationviews and 重叠注释的顺序 https://stackoverflow.com/questions/1145238/how-to-define-the-order-of-overlapping-mkannotationviewsstackoverflow 上的问题对我帮助不大。
欢迎任何想法(即使是肮脏的解决方案)!
这是我的一些代码(没什么花哨的,很常见):
自定义注释.h
@interface CustomAnnotation : NSObject <MKAnnotation> {
@private
CustomAnnotationView* view;
}
@property (nonatomic, retain) CustomAnnotationView* view;
@end
自定义注释.m
@implementation CustomAnnotation
@synthetize view;
自定义注释视图.h
@interface CustomAnnotationView : MKAnnotationView {
}
@end
自定义AnnotationView.m
@implementation CustomAnnotationView
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// Do something related to the annotation tapped
}
@end
主班
... // 添加了注释,其中一些注释与其他注释重叠。
- (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;
}