在 MkMapView 上隐藏、显示注释

Posted

技术标签:

【中文标题】在 MkMapView 上隐藏、显示注释【英文标题】:Hide, show annotation on MkMapView 【发布时间】:2010-01-20 10:08:52 【问题描述】:

缩小地图视图时如何隐藏注释。我有大量注释,我必须隐藏它们,因为如果地图上显示的区域太大,您只能看到注释。

【问题讨论】:

【参考方案1】:

为此,您必须检查您所在区域的大小,并根据它设置视图是否隐藏。

我测试了下面的代码,但是您可能需要进行一些调整。


- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

    NSArray *annotations = [_mapView annotations];  
    MyAnnotation *annotation = nil; 
    for (int i=0; i<[annotations count]; i++)
    
        annotation = (MyAnnotation*)[annotations objectAtIndex:i];
        if (_mapView.region.span.latitudeDelta > .010)
        
            [[_mapView viewForAnnotation:annotation] setHidden:YES];
        
        else 
            [[_mapView viewForAnnotation:annotation] setHidden:NO];
        
    

【讨论】:

感谢支持。+1 2022 年仍然有效 :-)!【参考方案2】:

Swift 版本:

let annotations = self.maps.annotations

    for annotation in annotations
    
        if (self.maps.region.span.latitudeDelta > 0.010)
        

            self.maps.viewForAnnotation(annotation)?.hidden = true

        
        else 

            self.maps.viewForAnnotation(annotation)?.hidden = false

        
    

【讨论】:

以上是关于在 MkMapView 上隐藏、显示注释的主要内容,如果未能解决你的问题,请参考以下文章