开发者

Hide, show annotation on MkMapView

开发者 https://www.devze.com 2022-12-16 19:41 出处:网络
How to hide annotation when zooming out the map view. I have a big number of annotation i have to hide them because if th开发者_JS百科e region displayed on the map is too big you can see only the anno

How to hide annotation when zooming out the map view. I have a big number of annotation i have to hide them because if th开发者_JS百科e region displayed on the map is too big you can see only the annotations.


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 to make 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];
        }
    }
}


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

        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消