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

[ios] keep callout view open when panning #6676

Merged
merged 1 commit into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
* Fixed an issue preventing MGLAnnotationView from animating when its coordinate changes. ([#6215](https://github.com/mapbox/mapbox-gl-native/pull/6215))
* Fixed an issue causing the wrong annotation view to be selected when tapping an annotation view with a center offset applied. ([#5931](https://github.com/mapbox/mapbox-gl-native/pull/5931))
* Fixed an issue that assigned annotation views to polyline and polygon annotations. ([#5770](https://github.com/mapbox/mapbox-gl-native/pull/5770))
* Fixed an issue causing the callout view to be dismissed when panning around. ([#6676](https://github.com/mapbox/mapbox-gl-native/pull/6676))
* Per documentation, the first and last coordinates in an MGLPolygon must be identical in order for the polygon to draw correctly. The same is true for an MGLPolygon’s interior polygon. ([#5514](https://github.com/mapbox/mapbox-gl-native/pull/5514))
* To make an MGLPolyline or MGLPolygon span the antimeridian, specify coordinates with longitudes greater than 180° or less than −180°. ([#6088](https://github.com/mapbox/mapbox-gl-native/pull/6088))
* Deprecated `-[MGLMapViewDelegate mapView:alphaForShapeAnnotation:]` in favor of specifying an alpha component via `-[MGLMapViewDelegate mapView:strokeColorForShapeAnnotation:]` or `-[MGLMapViewDelegate mapView:fillColorForPolygonAnnotation:]`. ([#6706](https://github.com/mapbox/mapbox-gl-native/pull/6706))
Expand Down
49 changes: 48 additions & 1 deletion platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4554,7 +4554,23 @@ - (void)notifyMapChange:(mbgl::MapChange)change
|| self.userTrackingMode == MGLUserTrackingModeNone
|| self.userTrackingState != MGLUserTrackingStateChanged)
{
[self deselectAnnotation:self.selectedAnnotation animated:NO];
// Deselect annotation if it lies outside the viewport
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is solving for something similar to -[MGLMapView updateAnnotationViews]. I wonder if checking if the -array -[MGLMapView visibleAnnotations] contains the selected annotation would be fast enough. If it is, it would simplify this implementation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already call -visibleAnnotations soon after this in order to reposition annotation views. We could refactor this code to call `-visibleAnnotations upfront and reuse the results for placement of annotation views, the callout view, and (on macOS) tooltip and cursor rects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think -visibleAnnotations is fine but -[NSArray containsObject:] is O(n2) and takes 2ms with 100 annotations. Not sure if it's in the beginning or the end but it would decrease the performance while panning around.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Ideally visibleAnnotations would be a set, but unfortunately the way we compute it (using feature querying) means it’s always going to be O(n) regardless. (Not O(n²), because we’re only checking whether one particular annotation is visible.)

if (self.selectedAnnotation) {
MGLAnnotationTag tag = [self annotationTagForAnnotation:self.selectedAnnotation];
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(tag);
MGLAnnotationView *annotationView = annotationContext.annotationView;

CGRect rect = [self positioningRectForCalloutForAnnotationWithTag:tag];

if (annotationView)
{
rect = annotationView.frame;
}

if ( ! CGRectIntersectsRect(rect, self.frame)) {
[self deselectAnnotation:self.selectedAnnotation animated:NO];
}
}
}

if ( ! [self isSuppressingChangeDelimiters] && [self.delegate respondsToSelector:@selector(mapView:regionWillChangeAnimated:)])
Expand Down Expand Up @@ -4653,6 +4669,7 @@ - (void)notifyMapChange:(mbgl::MapChange)change
[self.style didChangeValueForKey:@"layers"];
}
[self updateAnnotationViews];
[self updateCalloutView];
if ([self.delegate respondsToSelector:@selector(mapViewDidFinishRenderingFrame:fullyRendered:)])
{
[self.delegate mapViewDidFinishRenderingFrame:self fullyRendered:(change == mbgl::MapChangeDidFinishRenderingFrameFullyRendered)];
Expand Down Expand Up @@ -4788,6 +4805,36 @@ - (void)updateAnnotationViews
[CATransaction commit];
}

- (void)updateCalloutView
{
[CATransaction begin];

UIView <MGLCalloutView> *calloutView = self.calloutViewForSelectedAnnotation;
id <MGLAnnotation> annotation = calloutView.representedObject;

if (calloutView && annotation)
{
MGLAnnotationTag tag = [self annotationTagForAnnotation:annotation];
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(tag);
MGLAnnotationView *annotationView = annotationContext.annotationView;

CGRect rect = [self positioningRectForCalloutForAnnotationWithTag:tag];

if (annotationView)
{
rect = annotationView.frame;
}

CGPoint point = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));

if ( ! CGPointEqualToPoint(calloutView.center, point)) {
calloutView.center = point;
}
}

[CATransaction commit];
}

- (void)enqueueAnnotationViewForAnnotationContext:(MGLAnnotationContext &)annotationContext
{
MGLAnnotationView *annotationView = annotationContext.annotationView;
Expand Down