通过iPhone中的方法隐藏地图视图中的图钉/标记

时间:2012-01-27 16:34:43

标签: iphone ios mapkit

在我的viewDidLoad:方法中显示车辆数量的不同位置我使用了一些引脚/标记/注释。

我想知道的是,如果用户只选择一辆车,那么我该如何隐藏其他针/标记。

有可能吗?

怎么做?

1 个答案:

答案 0 :(得分:2)

是的,这是可能的。首先需要向每个annotationView添加一个观察者,以检测用户何时选择注释。首先,您必须添加:

static NSString * const GMAP_ANNOTATION_SELECTED = @"gMapAnnotationSelected";

到实现的顶部(在@implementation下面)。然后,您必须为每个annotationView添加一个观察者。这可以通过以下方式完成:

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

//Create your custom annotation called annotationView

[annotationView addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:(void*)("gMapAnnotationSelected")];

return annotationView;

然后将以下函数添加到选择annotationView时调用的同一文件中:

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)localContext {
char *actionCharStr = (char*)localContext;
NSString *action = [NSString stringWithCString:actionCharStr encoding:NSStringEncodingConversionAllowLossy];

if ([action isEqualToString:GMAP_ANNOTATION_SELECTED]) {
    BOOL annotationSelected = [[change valueForKey:@"new"] boolValue];

    CustomAnnotationView *customAnnotationView;
    if (annotationSelected) {
        currentAnnotationView = (CustomAnnotationView*)object;
        //Go through each annotation in [yourMap annotations] and remove from map if not equal to [currentAnnotationView annotation]
    } else {
        currentAnnotationView = (CustomAnnotationView*)object;
        //Add the annotations back once the annotation is no longer selected    
    }
}}

有关更多信息,请查看:
How to detect AnnotationView selected
MKMapViewDelegate