Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove existing annotation and add new annotation into the mapview

I am loading a mapview with a single annotation. Just like the "map app" in iphone. I have a search bar, which shows the address of the pin location, shown in map. Now I decide to change the location address. I type a new address location in the search bar. Then my mapview has to remove the existing annotation and add the new annotation. Right now, I am adding the new annotation, but I am unable to remove the existing annotation. How can I remove that existing annotation?

like image 782
Xavi Valero Avatar asked Nov 27 '11 06:11

Xavi Valero


1 Answers

First, get the list of annotations on the map view, and then remove the annotations that are not the current user location (i.e. in the MKUserLocation class).

NSArray *annotations = [mapView annotations];
for (id annotation in annotations) {
    if (annotation isKindOfClass:[MKUserLocation class]) {
        continue;
    }
    [mapView removeAnnotation:annotation];
}

Then you can add your new annotation as normal.

like image 77
Abizern Avatar answered Sep 28 '22 05:09

Abizern