Search code examples
iosannotationsmapkitpins

iOS Mapkit - How to filter annotations (show/hide)?


So I have 4 different types of annotations that show specific pins (i.e. colors). I want to be able to have a segmented control that allows users to show/hide these annotations by type.

Is there a simple way to hide specific annotations on command? Or do I really have to remove, then rebuild the annotations with only the ones I want shown?

This app could potentially have hundreds or thousands of pins, all of which will be 1 of the 4 different types of pins.

Thanks!


Solution

  • Figured it out.

    Basically when creating the annotations, I store them in different arrays (for each type).

    //Store Annotation in Array
        [type1Array addObject:annotation];
    

    Then, when my button is pressed to filter them, I use the following:

        [_mapView removeAnnotations:type1Array];
        [_mapView addAnnotations:type2Array];
    

    This seems to work fine and doesnt cause problems with duplicating annotations. I used an NSLog to output the _mapView.annotations.count to verify that the annotations were duplicating.

    Hope this helps others!