2010-03-18 98 views
9

在我的iphone应用程序中,我使用MapKit和MKMapView以及自定义的MKAnnotationView。在地图上重叠注释(MKAnnotationView)的问题

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

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

Z orderingOrder of overlapping annotations关于stackoverflow的问题并没有多大帮助。

欢迎任何想法(甚至肮脏的解决方案)!

下面是一些我的代码(没有什么花哨,很常见的):

CustomAnnotation.h

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

    @property (nonatomic, retain) CustomAnnotationView* view; 

@end 

CustomAnnotation.m

@implementation CustomAnnotation 

@synthetize view; 

CustomAnnotationView.h

@interface CustomAnnotationView : MKAnnotationView { 
} 

@end 

CustomAnnotationView.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; 
} 

回答

2

最后,我找到了最好的方式,避免失去地图funcionalities是:

  • 让上CustomAnnotationView(touchesEnded)触摸监听器(每个注释)

  • 当接收触摸事件,将事件转移到主类

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

它工作得很好。与

annView.enabled = NO 

+0

好主意。我做了透明UIView,发现它缺乏。 – 2010-04-24 18:01:16

+1

我很想知道你是如何处理第三点的。你如何找到某个CGPoint或CGRect的最前面的注释? – samvermette 2010-10-21 17:18:52

0

查看此解决方案question。基本上你会创建一个透明的UIView,它将拦截所有的触摸。我还没有尝试过,但如果你不得不重新实现MKMapView中的所有缩放功能,这可能比它的价值更麻烦。

如果您有两个(或更多)MKAnnotations共址,您可以重复点击以在两者之间切换。这应该没有任何改变你的应用程序的工作。

0

在亚历山大的回答跟进,我停用了所有的自定义MKAnnotationViews对象,使他们不至的MKMapView默认选择的行为作出反应,并在我的自定义实现MKAnnotationView如下:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
self.enabled = YES; 
[(MKMapView*)self.superview selectAnnotation:self.annotation animated:NO]; 
} 

这个解决方案在iOS 4上效果很好。看起来像禁用MKAnnotationView不会禁用它的userInteractionEnabled,而在iOS 3上它会。仍然在寻找适用于iOS 3的解决方案...

编辑:实际上,这似乎是iOS 4.1中的一个错误。在iOS 4.2中,禁用引脚也会禁用触摸事件。因此,此方法仅适用于4.1和可能的4.0。

0

我已将UITapGestureRecognizer添加到前面的视图中,并为我解决了问题。

在你的情况下,添加一个手势识别器到CustomAnnotationView应该可以解决这个问题。

+1

不要这样做。手势识别器阻止触摸事件传递到地图,不久地图停止进行代表回调,如regionDidChangeAnimated:等等,这会导致更多问题。 – Anurag 2012-05-14 18:06:47