Search code examples
iosobjective-cmkmapviewmkpointannotation

MKMapView add annotation and remove it after some time


I have a MKMapView and I add some annotations to it. I want to remove annotations one by one after some time that it was added. I need that every annotation have its own lifespan. Is it possible? How should I achieve that?


Solution

  • Your best choice is to use -(void)removeAnnotations:(NSArray *)annotations from your MKMapView.

    Just save your annotation somewhere, for example a NSDictionary with {date : annotationObject}, and retrieve it when you want to delete.

    For example:

    //Call somewhere to delete after 2 seconds
    [self performSelector:@selector(deleteAnnotation:) withObject:annotation afterDelay:2.f]
    
    //this function will remove the annotation from your map
    -(void) deleteAnnotation:(id) object{
        [self.map removeAnnotations:@[object]];
    }