summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-12-11 15:53:59 -0800
committerMinh Nguyễn <mxn@1ec5.org>2015-12-13 17:26:53 -0800
commit835bb6ab081c41c8cacb651d9d0b410ff8d37d02 (patch)
tree61f4bfaaf4a3c62f261074e6d4451a3d2cc32fc7 /platform
parent727dd61553ec4fab90c8c35cd4013897a9314e55 (diff)
downloadqtlocation-mapboxgl-835bb6ab081c41c8cacb651d9d0b410ff8d37d02.tar.gz
[osx] Sort candidate annotations by proximity to tap
Like 78a39c17fb6677442231945ddf8b73797080e2e1 for #3261 on iOS.
Diffstat (limited to 'platform')
-rw-r--r--platform/osx/src/MGLMapView.mm47
1 files changed, 30 insertions, 17 deletions
diff --git a/platform/osx/src/MGLMapView.mm b/platform/osx/src/MGLMapView.mm
index c563cf0ca9..5d3995b3eb 100644
--- a/platform/osx/src/MGLMapView.mm
+++ b/platform/osx/src/MGLMapView.mm
@@ -1587,14 +1587,6 @@ public:
[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.
@@ -1639,7 +1631,7 @@ public:
// 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) {
@@ -1667,6 +1659,17 @@ public:
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
@@ -1706,15 +1709,28 @@ public:
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;
@@ -1759,10 +1775,7 @@ public:
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