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

Commit

Permalink
[osx] Sort candidate annotations by proximity to tap
Browse files Browse the repository at this point in the history
Like 78a39c1 for #3261 on iOS.
  • Loading branch information
1ec5 committed Dec 12, 2015
1 parent 855d6f2 commit a1a0383
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions platform/osx/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1587,14 +1587,6 @@ - (void)removeAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations {
[self updateAnnotationTrackingAreas];
}

- (id <MGLAnnotation>)selectedAnnotation {
if (!_annotationContextsByAnnotationTag.count(_selectedAnnotationTag)) {
return nil;
}
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(_selectedAnnotationTag);
return annotationContext.annotation;
}

- (nullable MGLAnnotationImage *)dequeueReusableAnnotationImageWithIdentifier:(NSString *)identifier {
// This prefix is used to avoid collisions with style-defined sprites in
// mbgl, but reusable identifiers are never prefixed.
Expand Down Expand Up @@ -1639,7 +1631,7 @@ - (MGLAnnotationTag)annotationTagAtPoint:(NSPoint)point persistingResults:(BOOL)

// Filter out any annotation whose image is unselectable or for which
// hit testing fails.
mbgl::util::erase_if(nearbyAnnotations, [&](const MGLAnnotationTag annotationTag) {
std::remove_if(nearbyAnnotations.begin(), nearbyAnnotations.end(), [&](const MGLAnnotationTag annotationTag) {
NSAssert(_annotationContextsByAnnotationTag.count(annotationTag) != 0, @"Unknown annotation found nearby click");
id <MGLAnnotation> annotation = [self annotationWithTag:annotationTag];
if (!annotation) {
Expand Down Expand Up @@ -1667,6 +1659,17 @@ - (MGLAnnotationTag)annotationTagAtPoint:(NSPoint)point persistingResults:(BOOL)
std::sort(nearbyAnnotations.begin(), nearbyAnnotations.end());

if (nearbyAnnotations == _annotationsNearbyLastClick) {
CLLocationCoordinate2D currentCoordinate = [self convertPoint:point toCoordinateFromView:self];
std::sort(nearbyAnnotations.begin(), nearbyAnnotations.end(), [&](const MGLAnnotationTag tagA, const MGLAnnotationTag tagB) {
CLLocationCoordinate2D coordinateA = [[self annotationWithTag:tagA] coordinate];
CLLocationCoordinate2D coordinateB = [[self annotationWithTag:tagB] coordinate];
double distanceA = hypot(coordinateA.latitude - currentCoordinate.latitude,
coordinateA.longitude - currentCoordinate.longitude);
double distanceB = hypot(coordinateB.latitude - currentCoordinate.latitude,
coordinateB.longitude - currentCoordinate.longitude);
return distanceA < distanceB;
});

// The last time we persisted a set of annotations, we had the same
// set of annotations as we do now. Cycle through them.
if (_lastSelectedAnnotationTag == MGLAnnotationTagNotFound
Expand Down Expand Up @@ -1706,15 +1709,28 @@ - (MGLAnnotationTag)annotationTagAtPoint:(NSPoint)point persistingResults:(BOOL)
return _mbglMap->getPointAnnotationsInBounds(queryBounds);
}

- (id <MGLAnnotation>)selectedAnnotation {
if (!_annotationContextsByAnnotationTag.count(_selectedAnnotationTag)) {
return nil;
}
MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(_selectedAnnotationTag);
return annotationContext.annotation;
}

- (void)setSelectedAnnotation:(id <MGLAnnotation>)annotation {
[self willChangeValueForKey:@"selectedAnnotations"];
_selectedAnnotationTag = [self annotationTagForAnnotation:annotation];
if (_selectedAnnotationTag != MGLAnnotationTagNotFound) {
_lastSelectedAnnotationTag = _selectedAnnotationTag;
}
[self didChangeValueForKey:@"selectedAnnotations"];
}

- (NS_ARRAY_OF(id <MGLAnnotation>) *)selectedAnnotations {
id <MGLAnnotation> selectedAnnotation = self.selectedAnnotation;
return selectedAnnotation ? @[selectedAnnotation] : @[];
}

- (void)setSelectedAnnotation:(id <MGLAnnotation>)selectedAnnotation {
_selectedAnnotationTag = [self annotationTagForAnnotation:selectedAnnotation];
}

- (void)setSelectedAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)selectedAnnotations {
if (!selectedAnnotations.count) {
return;
Expand Down Expand Up @@ -1759,10 +1775,7 @@ - (void)selectAnnotation:(id <MGLAnnotation>)annotation
return;
}

[self willChangeValueForKey:@"selectedAnnotation"];
_selectedAnnotationTag = annotationTag;
_lastSelectedAnnotationTag = _selectedAnnotationTag;
[self didChangeValueForKey:@"selectedAnnotation"];
self.selectedAnnotation = annotation;

// For the callout to be shown, the annotation must have a title, its
// callout must not already be shown, and the annotation must be able to
Expand Down

0 comments on commit a1a0383

Please sign in to comment.