vishnu sivabalan
vishnu sivabalan

Reputation: 97

Remove a specific annotation from mapview

In my mapview I am creating pins like cafe, bar, night_club, and restaurant. How can I code to remove only the a specific point, cafe for example. I have used the code below to successfully remove ALL the annotations. I can't figure out how to delete one specific one (instead of all of them).

for (id<MKAnnotation> annotation in routeMapView.annotations) 
{
  if ([annotation isKindOfClass:[MapPoint class]]) 
  {
    [routeMapView removeAnnotation:index=annotation];
  }
}

Upvotes: 4

Views: 2581

Answers (2)

Avtar Guleria
Avtar Guleria

Reputation: 2136

You can use NSPredicate for the situation like

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pinName == %@",  @"Cafe"];
[myMapView removeAnnotations:[myMapView.annotations filteredArrayUsingPredicate:predicate]];

Above will remove all the annotations named cafe.

For more info you can see this answer Remove MKMapView Annotations with a certain pinColor?

Upvotes: 1

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

It may help you please try to use this one.

for (id annotation in mapView.annotations)
{
    if ([[annotation title] isEqualToString:@"cafe"])
          [self.mapView removeAnnotation:annotation];
}

Upvotes: 4

Related Questions