【问题标题】:Hiding map annotations without deleting them隐藏地图注释而不删除它们
【发布时间】:2017-08-11 09:18:10
【问题描述】:

使用 MKMapView 我加载了一堆注释,我希望能够过滤使用分段控件显示的注释。

我正在使用带有类型变量的自定义注释,因此我可以将它们彼此区分开,但我无法找到一种方法来随意隐藏和显示注释视图的子集。

【问题讨论】:

    标签: iphone objective-c annotations mapkit


    【解决方案1】:

    当然,试试这个:

    Objective-C解决方案:

    [[yourMapView viewForAnnotation:yourAnnotation] setHidden:YES]
    

    Swift 4解决方案:

    yourMapView.view(for: yourAnnotation)?.isHidden = true
    

    这将返回与指定注释对象关联的视图,然后您可以将视图设置为隐藏。这是documentation

    【讨论】:

    • 谢谢你,辛苦了。很抱歉没有早点回来。
    • 过滤时是隐藏注释还是删除注释更好?
    • 这将隐藏直到缩放/移动地图,不是完全如此,但不稳定,常见
    【解决方案2】:

    如果你想隐藏 MKAnnotationView(气泡),你可以创建一个自定义的:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
    
        if (annotation==self.map.mapView.userLocation)
            return nil;
    
    
        MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];
        if([annotation isKindOfClass:[AnnotationCustomClass class]] ) {
            annotationView.canShowCallout = NO; // <- hide the bubble
    
        }
    
        return annotationView;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 2014-05-25
      相关资源
      最近更新 更多