开发者

iOS - MKMapView show annotation only a certain zoom level

I have a MKMapView with some custom annotations that don't look that great when the map is zoom far out.

Is it possible to only show/add them 开发者_JAVA百科when the map is at a certain zoom level?


Using Marko's answer I came to this solution.

Everytime region changes, I change the ViewController's property isAtBigZoom.

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    isAtBigZoom = mapView.region.span.latitudeDelta < 0.01
}

Then at didSet of the property, I execute this code.

var isAtBigZoom = false {
    didSet {
        // this guard ensures, that the showing and hiding happens only once
        guard oldValue != isAtBigZoom else {
            return 
        }

        // in my case I wanted to show/hide only a certain type of annotations
        for case let annot as MapTextAnnotation in mapView.annotations {
            mapView.viewForAnnotation(annot)?.alpha = isAtBigZoom ? 1 : 0
        }
    }
}

If you also want to start with the annotations hidden, just add the alpha changing code to viewForAnnotation method.

Works great and I haven't noticed big issues with performance. Though that may change with the increasing number of annotations...


You can get the map zoom level via

[map region];

property of the MKMapView. also you get the notifications for region changing events by implementing the MKMapViewDelegate method and setting the delegate

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

here you can check what your current zoom level is. I don't recommend removing or adding all the annotations while zooming / panning since that could really effect the app performance. I haven't really tried setting alpha to 0.0 or hidden property on MKAnnotationView, but that could be your best bet.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜