Hide, show annotation on MkMapView

吃可爱长大的小学妹 提交于 2020-03-17 07:29:47

问题


How to hide annotation when zooming out the map view. I have a big number of annotation i have to hide them because if the region displayed on the map is too big you can see only the annotations.


回答1:


To do this, you have to check the size of your region, and depending of it you set the views hidden or not.

I tested the code bellow but, you will probably need some adjustments.


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

Cheers,
VFN




回答2:


Swift version:

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

        }
    }


来源:https://stackoverflow.com/questions/2100483/hide-show-annotation-on-mkmapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!