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

Commit

Permalink
[ios] break out callout view positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
frederoni committed Dec 1, 2016
1 parent 9fd09bc commit 5bffd38
Showing 1 changed file with 56 additions and 36 deletions.
92 changes: 56 additions & 36 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4650,6 +4650,9 @@ - (void)updateAnnotationViews

if (!delegateImplementsViewForAnnotation)
{
[CATransaction begin];
[self updateCalloutView];
[CATransaction commit];
return;
}

Expand Down Expand Up @@ -4702,43 +4705,9 @@ - (void)updateAnnotationViews
if (annotationView)
{
annotationView.center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self];
// Pin the callout view to the view annotation
UIView <MGLCalloutView> *calloutView = self.calloutViewForSelectedAnnotation;
if (calloutView && calloutView.representedObject == annotationContext.annotation) {
CGPoint point = annotationView.center;
point.x += annotationView.centerOffset.dx;
point.y += annotationView.centerOffset.dy;
point.y -= CGRectGetMidY(annotationView.bounds);

if ( ! CGPointEqualToPoint(calloutView.center, point))
{
calloutView.center = point;
}
}
} else {
// Pin the callout view to the gl annotation
UIView <MGLCalloutView> *calloutView = self.calloutViewForSelectedAnnotation;
if (calloutView && calloutView.representedObject == annotationContext.annotation) {
BOOL implementsImageForAnnotation = [self.delegate respondsToSelector:@selector(mapView:imageForAnnotation:)];
if (implementsImageForAnnotation) {
MGLAnnotationImage *image = [self imageOfAnnotationWithTag:annotationTag];
if (!image)
{
image = [self dequeueReusableAnnotationImageWithIdentifier:MGLDefaultStyleMarkerSymbolName];
}
if (image)
{
CGRect rect = [self positioningRectForCalloutForAnnotationWithTag:annotationTag];
CGRect insetRect = UIEdgeInsetsInsetRect(rect, image.image.alignmentRectInsets);
CGPoint point = CGPointMake(CGRectGetMidX(insetRect), CGRectGetMidY(insetRect));

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

[self updateCalloutView];
}

CGPoint upperLeft = {_largestAnnotationViewSize.width,_largestAnnotationViewSize.height};
Expand Down Expand Up @@ -4788,6 +4757,57 @@ - (void)updateAnnotationViews
[CATransaction commit];
}

- (void)updateCalloutView
{
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;

// Update callout view for view annotation
if (annotationView)
{
CGPoint point = annotationView.center;
point.x += annotationView.centerOffset.dx;
point.y += annotationView.centerOffset.dy;
point.y -= CGRectGetMidY(annotationView.bounds);

if ( ! CGPointEqualToPoint(calloutView.center, point))
{
calloutView.center = point;
}
}
else // Update callout view for gl annotation
{
MGLAnnotationImage *image = [self imageOfAnnotationWithTag:tag];

if (!image && [self.delegate respondsToSelector:@selector(mapView:imageForAnnotation:)]) {
image = [self.delegate mapView:self imageForAnnotation:annotation];
}

if (!image)
{
image = [self dequeueReusableAnnotationImageWithIdentifier:MGLDefaultStyleMarkerSymbolName];
}

if (image)
{
CGRect rect = [self positioningRectForCalloutForAnnotationWithTag:tag];
CGRect insetRect = UIEdgeInsetsInsetRect(rect, image.image.alignmentRectInsets);
CGPoint point = CGPointMake(CGRectGetMidX(insetRect), CGRectGetMidY(insetRect));

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

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

0 comments on commit 5bffd38

Please sign in to comment.