John S.
John S.

Reputation: 21

How to remove a certain annotation pin

I've got an application that adds annotation points to a mapView like so:

annot = [[AnnotationDelegate alloc] init];
annot.coordinate = CLLocationCoordinate2DMake(lat,long);
annot.title = [NSString stringWithFormat:@"%d",id];
annot.subtitle = string;
[mapView addAnnotation: annot]; 

This block of code can be executed several times, as I allow the user to add as many pins as they'd like to the mapView. My question is, would there be a way to modify this so that the user could REMOVE a certain pin? Right now I can only seem to remove the one most recently added.

Would appreciate any help, thanks.

Upvotes: 2

Views: 3264

Answers (1)

Roger
Roger

Reputation: 15813

Just remove the annotation using

[mapView removeAnnotation:annotationToRemove]

Presumably you have some form of UI to enable the user to choose which one they are dealing with? For example you could have a scenario where they tap a pin to select it, and then tap a delete button elsewhere in your UI to remove that pin? You could track which one was last selected using something a bit like this;

-(void) mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

    // Do other stuff
    annotationToRemove = view.annotation;    
 }

You can also implement the didDeselectAnnotaionView method as well.

As always, there's copious documentation over at Apple

Upvotes: 2

Related Questions