Hide annotations without deleting them

Using MKMapView I have a bunch of annotations uploaded to it, and I want to be able to filter annotations displayed using a segmented control.

I use custom annotations with a variable of type, so I can tell them separately from each other, but I could not find a way to hide and show a subset of the annotation views of my choice.

+6
objective-c iphone annotations mapkit
source share
2 answers

Of course, try the following:

Objective-C :

[[yourMapView viewForAnnotation:yourAnnotation] setHidden:YES] 

Swift 4 solution :

 yourMapView.view(for: yourAnnotation)?.isHidden = true 

This will return you the view associated with the specified annotation object, after which you can hide the view. Here is the documentation .

+8
source share

if you want to hide the MKAnnotationView (bubble), you can create your own:

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { if (annotation==self.map.mapView.userLocation) return nil; MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"]; if([annotation isKindOfClass:[AnnotationCustomClass class]] ) { annotationView.canShowCallout = NO; // <- hide the bubble } return annotationView; } 
0
source share

All Articles