summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2016-11-07 23:04:52 -0800
committerMinh Nguyễn <mxn@1ec5.org>2016-11-08 22:51:05 -0800
commitf2ba2b2deea7f832e575db652851dc7f5787309d (patch)
tree578adec77968b00439fe4a75531ce329c4150970
parentb91bbbef10525ba183f3ba23ffe9e57ed829db21 (diff)
downloadqtlocation-mapboxgl-f2ba2b2deea7f832e575db652851dc7f5787309d.tar.gz
[ios, macos] Fixed crash getting annotation view
Streamlined -[MGLMapView annotationTagForAnnotation:] to use the mapping added in #5987. Reverted an unsafe access into that mapping that was added in #5987; returning nil is preferable when no such annotation can be found. Return the user location annotation view when given the user location annotation.
-rw-r--r--platform/ios/src/MGLMapView.mm32
-rw-r--r--platform/macos/src/MGLMapView.mm9
2 files changed, 19 insertions, 22 deletions
diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm
index 00d2425a47..39f243f57c 100644
--- a/platform/ios/src/MGLMapView.mm
+++ b/platform/ios/src/MGLMapView.mm
@@ -2865,19 +2865,13 @@ public:
/// Returns the annotation tag assigned to the given annotation. Relatively expensive.
- (MGLAnnotationTag)annotationTagForAnnotation:(id <MGLAnnotation>)annotation
{
- if ( ! annotation || annotation == self.userLocation)
+ if ( ! annotation || annotation == self.userLocation
+ || _annotationTagsByAnnotation.count(annotation) == 0)
{
return MGLAnnotationTagNotFound;
}
-
- for (auto &pair : _annotationContextsByAnnotationTag)
- {
- if (pair.second.annotation == annotation)
- {
- return pair.first;
- }
- }
- return MGLAnnotationTagNotFound;
+
+ return _annotationTagsByAnnotation.at(annotation);
}
- (void)addAnnotation:(id <MGLAnnotation>)annotation
@@ -3101,7 +3095,14 @@ public:
- (nullable MGLAnnotationView *)viewForAnnotation:(id<MGLAnnotation>)annotation
{
- MGLAnnotationTag annotationTag = _annotationTagsByAnnotation.at(annotation);
+ if (annotation == self.userLocation)
+ {
+ return self.userLocationAnnotationView;
+ }
+ MGLAnnotationTag annotationTag = [self annotationTagForAnnotation:annotation];
+ if (annotationTag == MGLAnnotationTagNotFound) {
+ return nil;
+ }
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag);
return annotationContext.annotationView;
}
@@ -4675,9 +4676,9 @@ public:
continue;
}
- // Get the annotation tag then use it to get the context. This avoids the expensive lookup
- // by tag in `annotationTagForAnnotation:`
- MGLAnnotationTag annotationTag = _annotationTagsByAnnotation.at(annotation);
+ // Get the annotation tag then use it to get the context.
+ MGLAnnotationTag annotationTag = [self annotationTagForAnnotation:annotation];
+ NSAssert(annotationTag != MGLAnnotationTagNotFound, @"-visibleAnnotationsInRect: returned unrecognized annotation");
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag);
MGLAnnotationView *annotationView = annotationContext.annotationView;
@@ -4721,7 +4722,8 @@ public:
continue;
}
- MGLAnnotationTag annotationTag = _annotationTagsByAnnotation.at(annotation);
+ MGLAnnotationTag annotationTag = [self annotationTagForAnnotation:annotation];
+ NSAssert(annotationTag != MGLAnnotationTagNotFound, @"-visibleAnnotationsInRect: returned unrecognized annotation");
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag);
UIView *annotationView = annotationContext.annotationView;
diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm
index bacba265f5..c50d3d5642 100644
--- a/platform/macos/src/MGLMapView.mm
+++ b/platform/macos/src/MGLMapView.mm
@@ -1667,16 +1667,11 @@ public:
/// Returns the annotation tag assigned to the given annotation. Relatively expensive.
- (MGLAnnotationTag)annotationTagForAnnotation:(id <MGLAnnotation>)annotation {
- if (!annotation) {
+ if (!annotation || _annotationTagsByAnnotation.count(annotation) == 0) {
return MGLAnnotationTagNotFound;
}
- for (auto &pair : _annotationContextsByAnnotationTag) {
- if (pair.second.annotation == annotation) {
- return pair.first;
- }
- }
- return MGLAnnotationTagNotFound;
+ return _annotationTagsByAnnotation.at(annotation);
}
- (void)addAnnotation:(id <MGLAnnotation>)annotation {