IOS - MKMapView shows annotation of only a specific zoom level

I have MKMapView with some custom annotations that don't look great when the map is zoomed in.

Is it possible to show / add them only when the map is at a certain zoom level?

+4
source share
2 answers

You can get the map zoom level with

[map region]; 

MKMapView properties. you also receive notifications of regional change events by implementing the MKMapViewDelegate method and setting a delegate

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

here you can check what your current zoom level is. I do not recommend deleting or adding all annotations during zooming / panning, as this can really affect application performance. I have not tried to set the alpha value to 0.0 or the hidden property on MKAnnotationView, but this might be your best bet.

+3
source

Using Marco's answer, I came to this solution.

Every time a region changes, I change the ViewController's isAtBigZoom .

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

Then in the didSet 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 hidden annotations, just add the alpha change code to the viewForAnnotation method.

Works great, and I didn't notice any big performance issues. Although this may change with an increase in the number of annotations ...

+4
source

Source: https://habr.com/ru/post/1343689/


All Articles