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?
MKMapView add annotation and remove it after some time
408 Views Asked by Roo AtThere are 2 best solutions below
On
MKMapView has removeAnnotation and removeAnnotations methods which remove already added annotations.
If you want each annotation to have their own lifespan :
Create
AnnotationLifespanDelegateprotocol with method for indicating end of life which takes the annotation as a parameter (e.g.func dearAnnotationRIP(annotation : MKAnnotation)).Create a custom MKAnnotation with the
lifespanandlifeSpanDelegateproperties and astartCountdownmethod.startCountdownmethod simply starts a timer with with an interval equal to thelifespanand calls the end of life method on the delegate.Implement
AnnotationLifespanDelegatemethods in view controller and while creating annotation objects, make sure you set thelifespanand thedelegateand call thestartCountdownmethod on the annotation immediately after you add it to the map view.In the annotation end of life method, remove annotation from the map.
Your best choice is to use
-(void)removeAnnotations:(NSArray *)annotationsfrom yourMKMapView.Just save your annotation somewhere, for example a NSDictionary with {date : annotationObject}, and retrieve it when you want to delete.
For example: