diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 25a9b8518..00d2425a4 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -2836,7 +2836,11 @@ public: for (auto const& annotationTag: annotationTags) { - MGLAnnotationContext annotationContext = _annotationContextsByAnnotationTag[annotationTag]; + if (!_annotationContextsByAnnotationTag.count(annotationTag)) + { + continue; + } + MGLAnnotationContext annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); [annotations addObject:annotationContext.annotation]; } @@ -2903,6 +2907,12 @@ public: { NSAssert([annotation conformsToProtocol:@protocol(MGLAnnotation)], @"annotation should conform to MGLAnnotation"); + // adding the same annotation object twice is a no-op + if ([self.annotations containsObject:annotation]) + { + continue; + } + if ([annotation isKindOfClass:[MGLMultiPoint class]]) { // The polyline or polygon knows how to style itself (with the map view’s help). diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index 4df365378..6679aaaa6 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -1635,7 +1635,11 @@ public: for (auto const& annotationTag: annotationTags) { - MGLAnnotationContext annotationContext = _annotationContextsByAnnotationTag[annotationTag]; + if (!_annotationContextsByAnnotationTag.count(annotationTag)) + { + continue; + } + MGLAnnotationContext annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); [annotations addObject:annotationContext.annotation]; } @@ -1687,6 +1691,12 @@ public: for (id annotation in annotations) { NSAssert([annotation conformsToProtocol:@protocol(MGLAnnotation)], @"Annotation does not conform to MGLAnnotation"); + // adding the same annotation object twice is a no-op + if ([self.annotations containsObject:annotation]) + { + continue; + } + if ([annotation isKindOfClass:[MGLMultiPoint class]]) { // The multipoint knows how to style itself (with the map view’s help). MGLMultiPoint *multiPoint = (MGLMultiPoint *)annotation;