Deleting duplicates in an array of annotations
I have a MKMapKit I'm populating with annotations using data I'm fetching
from an API. Each annotation has a title, description, URL, and
coordinates. I have a button I added to a navigation bar to fetch more
results and populate more annotations. The problem is when the API runs of
out new results in populates the map with duplicates of annotations that
were already fetched. I'm trying to delete duplicate annotations from an
array using an if statement but it's not working. Any suggestions? Thanks
in advance.
-(void)layAnnotations
{
if (self.annotations) {
[self.mapView removeAnnotations:self.annotations];
}
self.annotations = [NSMutableArray array];
for (Object *aObject in self.objectArray) {
CLLocationCoordinate2D coordinate;
coordinate.latitude = [aObject.latitude floatValue];
coordinate.longitude = [aObject.longitude floatValue];
Annotations *annotation = [[Annotations alloc] init];
annotation.title = aObject.objectTitle;
annotation.subtitle = aObject.description;
annotation.url = aObject.url;
annotation.coordinate = coordinate;
//attempting to filter duplicates here
if (![self.annotations containsObject:annotation]) {
[self.annotations addObject:annotation];
}
annotation = nil;
}
[self mutateCoordinatesOfClashingAnnotations:self.annotations];
[self.mapView addAnnotations:self.annotations];
}
No comments:
Post a Comment