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

[ios] fixes #5128 selected property to MGLAnnotationView #5283

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions platform/ios/app/MBXAnnotationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ - (void)setCenterColor:(UIColor *)centerColor {
}
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

self.layer.borderColor = selected ? [UIColor blackColor].CGColor : [UIColor whiteColor].CGColor;
self.layer.borderWidth = selected ? 2.0 : 0;
}

@end
6 changes: 6 additions & 0 deletions platform/ios/src/MGLAnnotationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, assign, getter=isFlat) BOOL flat;

/**
Defaults to NO. Becomes YES when clicked on in the map view.
Copy link
Contributor

Choose a reason for hiding this comment

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

On iOS, we say "tap" instead of "click".

*/
@property (nonatomic, assign, getter=isSelected) BOOL selected;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;

/**
Setting this property to YES will cause the annotation view to shrink as it approaches the horizon and grow as it moves away from the
horizon when the associated map view is tilted. Conversely, setting this property to NO will ensure that the annotation view maintains
Expand Down
10 changes: 10 additions & 0 deletions platform/ios/src/MGLAnnotationView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ - (void)setCenterOffset:(CGVector)centerOffset
self.center = self.center;
}

- (void)setSelected:(BOOL)selected
{
[self setSelected:selected animated:NO];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
_selected = selected;
Copy link
Contributor

Choose a reason for hiding this comment

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

In order to be KVO-compliant, we need to bracket this line with calls to -willChangeValueForKey: and -didChangeValueForKey:.

}

- (void)setCenter:(CGPoint)center
{
[self setCenter:center pitch:0];
Expand Down
17 changes: 16 additions & 1 deletion platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3376,7 +3376,7 @@ - (void)selectAnnotation:(id <MGLAnnotation>)annotation animated:(BOOL)animated
self.userTrackingMode = MGLUserTrackingModeNone;
}

[self deselectAnnotation:self.selectedAnnotation animated:NO];
[self deselectAnnotation:self.selectedAnnotation animated:animated];

// Add the annotation to the map if it hasn’t been added yet.
MGLAnnotationTag annotationTag = [self annotationTagForAnnotation:annotation];
Expand All @@ -3401,6 +3401,8 @@ - (void)selectAnnotation:(id <MGLAnnotation>)annotation animated:(BOOL)animated
positioningRect = annotationView.frame;

[annotationView.superview bringSubviewToFront:annotationView];

[annotationView setSelected:YES animated:animated];
Copy link
Contributor

Choose a reason for hiding this comment

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

The animated property was intended to determine whether the map view pans to the selected annotation with animation: #3249. I don't think MapKit uses it to determine whether other things like the annotation view's visual appearance should animate.

}
}

Expand Down Expand Up @@ -3554,6 +3556,19 @@ - (void)deselectAnnotation:(id <MGLAnnotation>)annotation animated:(BOOL)animate
{
// dismiss popup
[self.calloutViewForSelectedAnnotation dismissCalloutAnimated:animated];

// deselect annotation view
MGLAnnotationTag annotationTag = [self annotationTagForAnnotation:annotation];
if (annotationTag != MGLAnnotationTagNotFound)
{
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag);
MGLAnnotationView *annotationView = annotationContext.annotationView;

if (annotationView)
{
[annotationView setSelected:NO animated:animated];
}
}

// clean up
self.calloutViewForSelectedAnnotation = nil;
Expand Down