在地图上重叠注释(MKAnnotationView)的问题 [英] Issue with overlapping annotations (MKAnnotationView) on map

查看:136
本文介绍了在地图上重叠注释(MKAnnotationView)的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my iphone application, I'm using MapKit with MKMapView and custom MKAnnotationView.

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

The problem is when annotations overlap on map (in my app, annotations are photos and those photos may overlap) and when you tap on the annotation that appears on front, it's another annotation (on back) which receives the event (seems to be random).

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

I didn't find any way to send the event to the front annotation. I cannot believe this bug/problem doesn't have any solution!

Z订购 stackoverflow上的重叠注释顺序问题对我没那么大帮助。

Z ordering and Order of overlapping annotations questions on stackoverflow did not help me that much.

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

Please any idea is welcome (even dirty solutions)!

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

Here's some of my code (nothing fancy, very common):

CustomAnnotation.h

CustomAnnotation.h

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

    @property (nonatomic, retain) CustomAnnotationView* view;

@end

CustomAnnotation.m

CustomAnnotation.m

@implementation CustomAnnotation

@synthetize view;

CustomAnnotationView.h

CustomAnnotationView.h

@interface CustomAnnotationView : MKAnnotationView {
}

@end

CustomAnnotationView.m

CustomAnnotationView.m

@implementation CustomAnnotationView

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

@end

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

Main class ... // Annotations are added and some of them overlaps with others.

- (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;
}


推荐答案

最后,我最好的方式找到并避免丢失地图功能是:

Finally, the best way I found and that avoids loosing map funcionalities is to:


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

  • let the touch listener on CustomAnnotationView (touchesEnded) (on each annotation)

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

when receiving touch event, transfer the event to main class

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

main class loops on annotations and keeps the annotation that has a good location (touch event in the annotation frame) and that is in front

它运作得很好。

这篇关于在地图上重叠注释(MKAnnotationView)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆