Delete existing annotation and add new annotation in mapview

I load mapview with one annotation. Just like the β€œmap app” in iphone. I have a search bar that shows the pin location address shown on the map. Now I decided to change the location address. I type the new address location in the search bar. Then my map should delete the existing annotation and add a new annotation. I am adding a new annotation now, but I cannot delete an existing annotation. How to delete an existing annotation?

+6
source share
2 answers

First, get a list of annotations on the map screen, and then delete annotations that are not the user's current 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 usual.

+8
source

If you are using custom annotations, another way might be as follows:

 for (int i =0; i < [mapView.annotations count]; i++) {  if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotationClass class]]) {                [mapView removeAnnotation:[mapView.annotations objectAtIndex:i]];    }  } 

Therefore, delete only added annotations.

0
source

Source: https://habr.com/ru/post/902473/


All Articles