Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit fb759e8

Browse files
committed
[ios] Refactor of annotation view updates
This change eliminates the call to the problematic `annotationTagsInRect:` by using the annotation views themselves and the map view to check if a view is visible or not.
1 parent 3966924 commit fb759e8

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

platform/ios/src/MGLMapView.mm

+13-32
Original file line numberDiff line numberDiff line change
@@ -4364,56 +4364,37 @@ - (void)updateAnnotationViews
43644364
return;
43654365
}
43664366

4367-
// Update all visible annotation views
4368-
std::set<MGLAnnotationTag> visibleTags;
4369-
std::vector<MGLAnnotationTag> annotationTags = [self annotationTagsInRect:CGRectInset(self.bounds, -_largestAnnotationViewSize.width - MGLAnnotationUpdateViewportOutset.width, -_largestAnnotationViewSize.height - MGLAnnotationUpdateViewportOutset.width)];
4370-
4371-
for(auto const& annotationTag: annotationTags)
4367+
for (auto &pair : _annotationContextsByAnnotationTag)
43724368
{
4373-
auto &annotationContext = _annotationContextsByAnnotationTag[annotationTag];
4374-
id<MGLAnnotation> annotation = annotationContext.annotation;
4369+
CGRect viewPort = CGRectInset(self.bounds, -_largestAnnotationViewSize.width - MGLAnnotationUpdateViewportOutset.width, -_largestAnnotationViewSize.height - MGLAnnotationUpdateViewportOutset.width);
43754370

4371+
MGLAnnotationContext &annotationContext = pair.second;
4372+
MGLAnnotationView *annotationView = annotationContext.annotationView;
43764373

4377-
// If there is no annotation view at this point, it means the context's view was reused by some
4378-
// other context so we need to reuse or make a new view.
4379-
if (!annotationContext.annotationView)
4374+
if (!annotationView)
43804375
{
4381-
MGLAnnotationView *annotationView = [self annotationViewForAnnotation:annotation];
4382-
4376+
MGLAnnotationView *annotationView = [self annotationViewForAnnotation:annotationContext.annotation];
43834377
if (annotationView)
43844378
{
43854379
// If the annotation view has no superview it means it was never used before so add it
43864380
if (!annotationView.superview)
43874381
{
43884382
[self.glView addSubview:annotationView];
43894383
}
4390-
4384+
annotationView.center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self];
43914385
annotationContext.annotationView = annotationView;
43924386
}
43934387
}
43944388

4395-
annotationContext.annotationView.center = [self convertCoordinate:annotation.coordinate toPointToView:self];;
4396-
visibleTags.insert(annotationTag);
4397-
}
4398-
4399-
// Hide and add offscreen annotation views to reuse queue
4400-
for (auto &pair : _annotationContextsByAnnotationTag)
4401-
{
4402-
MGLAnnotationTag annotationTag = pair.first;
4403-
MGLAnnotationContext &annotationContext = pair.second;
4404-
MGLAnnotationView *annotationView = annotationContext.annotationView;
4405-
const bool tagIsNotVisible = visibleTags.find(annotationTag) == visibleTags.end();
4406-
4407-
// The call to `annotationTagsInRect:` (above) does not return the correct result when the
4408-
// map is tilted and the user is scrolling quickly. So, some annotation views get stuck in
4409-
// a limbo state where they are onscreen and put on the reuse queue. Hiding the views hides
4410-
// the bug until we fix the result of `annotationTagsInRect:`.
4411-
annotationView.hidden = tagIsNotVisible;
4412-
4413-
if (annotationView && annotationView.reuseIdentifier && tagIsNotVisible)
4389+
bool annotationViewIsVisible = CGRectContainsRect(viewPort, annotationView.frame);
4390+
if (!annotationViewIsVisible)
44144391
{
44154392
[self enqueueAnnotationViewForAnnotationContext:annotationContext];
44164393
}
4394+
else
4395+
{
4396+
annotationView.center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self];;
4397+
}
44174398
}
44184399
}
44194400

0 commit comments

Comments
 (0)