Check if mapView already contains annotation

I have a method of adding secondary annotations (ann2) when I use another annotation (ann1). But when I deselect and reselect the same annotation (ann1), ann2 creates it again and is added again. Is there a way to check if an existing annotation exists on the map, and if so, do not add anything to the new annotation. I already checked this: Limit duplicate annotation on MapView , but that didn't help me. Any advice is appreciated. This is what I still have:

fixedLocationsPin *pin = [[fixedLocationsPin alloc] init]; pin.title = [NSString stringWithFormat:@"%@",nearestPlace]; pin.subtitle = pinSubtitle; pin.coordinate = CLLocationCoordinate2DMake(newObject.lat, newObject.lon); for (fixedLocationsPin *pins in mapView.annotations) { if (MKMapRectContainsPoint(mapView.visibleMapRect, MKMapPointForCoordinate (pins.coordinate))) { NSLog(@"already in map"); }else{ [mapView addAnnotation:pin]; } 

In this case, I get the log already on the map, but I also get a drop animation adding annotations to the map. Any ideas?

Thanks in advance.

+7
source share
5 answers

Your for loop does not check if there is an annotation on the screen, it checks if the output coordinates are within the visible area. Even if he checked whether the pin object was already in mapView.annotations , it will never be true, because you just created a pin few lines before, it cannot be the same object as on mapView.annotations . It can have the same coordinates and title, and what you need to check:

 bool found = false; for (fixedLocationsPin *existingPin in mapView.annotations) { if (([existingPin.title isEqualToString:pin.title] && (existingPin.coordinate.latitude == pin.coordinate.latitude) (existingPin.coordinate.longitude == pin.coordinate.longitude)) { NSLog(@"already in map"); found = true; break; } } if (!found) { [mapView addAnnotation:pin]; } 
+3
source

Annotations exist in the map object, so you just need to check

 if ( yourmap.annotations.count==0) { NSLog(@"no annotations"); } 
0
source
 NSNumber *latCord = [row valueForKey:@"latitude"]; NSNumber *longCord = [row valueForKey:@"longitude"]; NSString *title = [row valueForKey:@"name"]; CLLocationCoordinate2D coord; coord.latitude = latCord.doubleValue; coord.longitude = longCord.doubleValue; MapAnnotation *annotation = [[MapAnnotation alloc]initWithCoordinate:coord withTitle:title]; if([mkMapView.annotations containsObject:annotation]==YES){ //add codes here if the map contains the annotation. }else { //add codes here if the annotation does not exist in the map. } 
0
source
  if (sampleMapView.annotations.count > 0) { sampleMapView.removeAnnotation(detailMapView.annotations.last!) } 
0
source

Following my comment on Craig's answer, I think the solution might look something like this:

 import MapKit extension MKMapView { func containsAnnotation(annotation: MKAnnotation) -> Bool { if let existingAnnotations = self.annotations as? [MKAnnotation] { for existingAnnotation in existingAnnotations { if existingAnnotation.title == annotation.title && existingAnnotation.coordinate.latitude == annotation.coordinate.latitude && existingAnnotation.coordinate.longitude == annotation.coordinate.longitude { return true } } } return false } } 

This code allows you to check if mapView contains this annotation. Use this in the for loop in all of your annotations:

 for annotation in annotations { if mapView.containsAnnotation(annotation) { // do nothing } else { mapView.addAnnotation(annotation) } 

PS: this works well if you need to add new annotations to mapView. But if you also need to delete entries, you may need to do the opposite: make sure that every existing annotation exists in a new array of annotations; if not, delete it. Or you can delete everything and add everything again (but then you will have an animated change ...)

-one
source

All Articles