This repository was archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[ios] Guard against looking up annotation contexts MGLAnnotationTagNotFound #8686
Merged
boundsj
merged 3 commits into
release-ios-v3.5.0-android-v5.0.0
from
boundsj-annotation-not-found-guard
Apr 7, 2017
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1452,7 +1452,9 @@ - (void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTap | |
} | ||
else | ||
{ | ||
nextElement = _annotationContextsByAnnotationTag.at(_selectedAnnotationTag).accessibilityElement; | ||
if (_selectedAnnotationTag != MGLAnnotationTagNotFound) { | ||
nextElement = _annotationContextsByAnnotationTag.at(_selectedAnnotationTag).accessibilityElement; | ||
} | ||
} | ||
[self deselectAnnotation:self.selectedAnnotation animated:YES]; | ||
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nextElement); | ||
|
@@ -2002,19 +2004,21 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N | |
{ | ||
const mbgl::Point<double> point = MGLPointFromLocationCoordinate2D(annotation.coordinate); | ||
|
||
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); | ||
if (annotationContext.annotationView) | ||
{ | ||
// Redundantly move the associated annotation view outside the scope of the animation-less transaction block in -updateAnnotationViews. | ||
annotationContext.annotationView.center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self]; | ||
} | ||
if (annotationTag != MGLAnnotationTagNotFound) { | ||
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); | ||
if (annotationContext.annotationView) | ||
{ | ||
// Redundantly move the associated annotation view outside the scope of the animation-less transaction block in -updateAnnotationViews. | ||
annotationContext.annotationView.center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self]; | ||
} | ||
|
||
MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag]; | ||
NSString *symbolName = annotationImage.styleIconIdentifier; | ||
MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag]; | ||
NSString *symbolName = annotationImage.styleIconIdentifier; | ||
|
||
// Update the annotation’s backing geometry to match the annotation model object. Any associated annotation view is also moved by side effect. However, -updateAnnotationViews disables the view’s animation actions, because it can’t distinguish between moves due to the viewport changing and moves due to the annotation’s coordinate changing. | ||
_mbglMap->updateAnnotation(annotationTag, mbgl::SymbolAnnotation { point, symbolName.UTF8String }); | ||
[self updateCalloutView]; | ||
// Update the annotation’s backing geometry to match the annotation model object. Any associated annotation view is also moved by side effect. However, -updateAnnotationViews disables the view’s animation actions, because it can’t distinguish between moves due to the viewport changing and moves due to the annotation’s coordinate changing. | ||
_mbglMap->updateAnnotation(annotationTag, mbgl::SymbolAnnotation { point, symbolName.UTF8String }); | ||
[self updateCalloutView]; | ||
} | ||
} | ||
} | ||
else if ([keyPath isEqualToString:@"coordinates"] && [object isKindOfClass:[MGLMultiPoint class]]) | ||
|
@@ -3055,17 +3059,18 @@ - (void)removeStyleClass:(NSString *)styleClass | |
|
||
for (auto const& annotationTag: annotationTags) | ||
{ | ||
if (!_annotationContextsByAnnotationTag.count(annotationTag)) | ||
if (!_annotationContextsByAnnotationTag.count(annotationTag) || | ||
annotationTag == MGLAnnotationTagNotFound) | ||
{ | ||
continue; | ||
} | ||
|
||
MGLAnnotationContext annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); | ||
NSAssert(annotationContext.annotation, @"Missing annotation for tag %u.", annotationTag); | ||
if (annotationContext.annotation) | ||
{ | ||
[annotations addObject:annotationContext.annotation]; | ||
} | ||
|
||
} | ||
|
||
return [annotations copy]; | ||
|
@@ -3077,8 +3082,8 @@ - (void)removeStyleClass:(NSString *)styleClass | |
/// Returns the annotation assigned the given tag. Cheap. | ||
- (id <MGLAnnotation>)annotationWithTag:(MGLAnnotationTag)tag | ||
{ | ||
if ( ! _annotationContextsByAnnotationTag.count(tag)) | ||
{ | ||
if ( ! _annotationContextsByAnnotationTag.count(tag) || | ||
tag == MGLAnnotationTagNotFound) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never mind. The code used to overload MGLAnnotationTagNotFound to mean the user location annotation tag. But in retrospect that's a pretty risky practice. This change moves us further away from that practice, forcing us to explicitly account for the user location annotation explicitly, which is a good thing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return nil; | ||
} | ||
|
||
|
@@ -3680,10 +3685,12 @@ - (MGLAnnotationTag)annotationTagAtPoint:(CGPoint)point persistingResults:(BOOL) | |
{ | ||
return self.userLocation; | ||
} | ||
if ( ! _annotationContextsByAnnotationTag.count(_selectedAnnotationTag)) | ||
{ | ||
|
||
if ( ! _annotationContextsByAnnotationTag.count(_selectedAnnotationTag) || | ||
_selectedAnnotationTag == MGLAnnotationTagNotFound) { | ||
return nil; | ||
} | ||
|
||
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(_selectedAnnotationTag); | ||
return annotationContext.annotation; | ||
} | ||
|
@@ -3749,19 +3756,16 @@ - (void)selectAnnotation:(id <MGLAnnotation>)annotation animated:(BOOL)animated | |
MGLAnnotationView *annotationView = nil; | ||
|
||
if (annotation != self.userLocation) | ||
{ | ||
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); | ||
|
||
annotationView = annotationContext.annotationView; | ||
|
||
if (annotationView && annotationView.enabled) | ||
{ | ||
// Annotations represented by views use the view frame as the positioning rect. | ||
positioningRect = annotationView.frame; | ||
|
||
[annotationView.superview bringSubviewToFront:annotationView]; | ||
|
||
[annotationView setSelected:YES animated:animated]; | ||
if (annotationTag != MGLAnnotationTagNotFound) { | ||
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); | ||
annotationView = annotationContext.annotationView; | ||
if (annotationView && annotationView.enabled) { | ||
{ | ||
// Annotations represented by views use the view frame as the positioning rect. | ||
positioningRect = annotationView.frame; | ||
[annotationView.superview bringSubviewToFront:annotationView]; | ||
[annotationView setSelected:YES animated:animated]; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -5039,8 +5043,12 @@ - (void)updateCalloutView | |
if (isAnchoredToAnnotation) | ||
{ | ||
MGLAnnotationTag tag = [self annotationTagForAnnotation:annotation]; | ||
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(tag); | ||
MGLAnnotationView *annotationView = annotationContext.annotationView; | ||
MGLAnnotationView *annotationView = nil; | ||
|
||
if (tag != MGLAnnotationTagNotFound) { | ||
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(tag); | ||
annotationView = annotationContext.annotationView; | ||
} | ||
|
||
CGRect rect = [self positioningRectForCalloutForAnnotationWithTag:tag]; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is correct with respect to the user location annotation, because we rely on Core Location, not KVO, to tell us when the model object has changed.