From 77951eea939e8f2b4bed00c04e50e4ebd2d5ed05 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Fri, 4 Sep 2015 17:36:30 -0500 Subject: [PATCH] #894 - Moving annotation selection determination to onSingleTapUp() to only run it when it's actual single tap. Fixing newSelectedAnnotationID to be greater than or equals to 0 to handle first annotation case --- .../com/mapbox/mapboxgl/views/MapView.java | 160 +++++++++--------- 1 file changed, 80 insertions(+), 80 deletions(-) diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java index ee1d21034..0e7c8a929 100644 --- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java @@ -973,86 +973,6 @@ public class MapView extends FrameLayout implements LocationListener { break; case MotionEvent.ACTION_UP: - - // Open / Close InfoWindow - PointF tapPoint = new PointF(event.getX(), event.getY()); - - float toleranceWidth = 60 * mScreenDensity; - float toleranceHeight = 80 * mScreenDensity; - - PointF tr = new PointF(tapPoint.x + toleranceWidth / 2, tapPoint.y + 2 * toleranceHeight / 3); - PointF bl = new PointF(tapPoint.x - toleranceWidth / 2, tapPoint.y - 1 * toleranceHeight / 3); - - LatLng sw = fromScreenLocation(bl); - LatLng ne = fromScreenLocation(tr); - - BoundingBox bbox = new BoundingBox(ne, sw); - - List nearbyAnnotations = getAnnotationsInBounds(bbox); - - long newSelectedAnnotationID = -1; - - if (nearbyAnnotations.size() > 0) { - - // there is at least one nearby annotation; select one - // - // first, sort for comparison and iteration - Collections.sort(nearbyAnnotations); - - if (nearbyAnnotations == mAnnotationsNearLastTap) - { - // the selection candidates haven't changed; cycle through them - if (mSelectedAnnotation != null && (mSelectedAnnotation.getId() == mAnnotationsNearLastTap.get(mAnnotationsNearLastTap.size() - 1).getId())) - { - // the selected annotation is the last in the set; cycle back to the first - // note: this could be the selected annotation if only one in set - newSelectedAnnotationID = mAnnotationsNearLastTap.get(0).getId(); - } - else if (mSelectedAnnotation != null) - { - // otherwise increment the selection through the candidates - long currentID = mSelectedAnnotation.getId(); - long result = mAnnotationsNearLastTap.indexOf(mSelectedAnnotation); - newSelectedAnnotationID = mAnnotationsNearLastTap.get((int) result + 1).getId(); - } - else - { - // no current selection; select the first one - newSelectedAnnotationID = mAnnotationsNearLastTap.get(0).getId(); - } - } - else - { - // start tracking a new set of nearby annotations - mAnnotationsNearLastTap = nearbyAnnotations; - - // select the first one - newSelectedAnnotationID = mAnnotationsNearLastTap.get(0).getId(); - } - - } else { - // there are no nearby annotations; deselect if necessary - newSelectedAnnotationID = -1; - } - - if (newSelectedAnnotationID > 0) { - - for (Annotation annotation : mAnnotations) { - if (annotation.getId() == newSelectedAnnotationID) { - if (annotation.getId() == mSelectedAnnotation.getId()) { - selectAnnotation(annotation); - } - break; - } - } - - } else { - // deselect any selected annotation - if (mSelectedAnnotation != null) { - deselectAnnotation(); - } - } - // First pointer up long tapInterval = event.getEventTime() - event.getDownTime(); boolean isTap = tapInterval <= ViewConfiguration.getTapTimeout(); @@ -1111,6 +1031,86 @@ public class MapView extends FrameLayout implements LocationListener { public boolean onSingleTapUp(MotionEvent e) { // Cancel any animation mNativeMapView.cancelTransitions(); + + // Open / Close InfoWindow + PointF tapPoint = new PointF(e.getX(), e.getY()); + + float toleranceWidth = 60 * mScreenDensity; + float toleranceHeight = 80 * mScreenDensity; + + PointF tr = new PointF(tapPoint.x + toleranceWidth / 2, tapPoint.y + 2 * toleranceHeight / 3); + PointF bl = new PointF(tapPoint.x - toleranceWidth / 2, tapPoint.y - 1 * toleranceHeight / 3); + + LatLng sw = fromScreenLocation(bl); + LatLng ne = fromScreenLocation(tr); + + BoundingBox bbox = new BoundingBox(ne, sw); + + List nearbyAnnotations = getAnnotationsInBounds(bbox); + + long newSelectedAnnotationID = -1; + + if (nearbyAnnotations.size() > 0) { + + // there is at least one nearby annotation; select one + // + // first, sort for comparison and iteration + Collections.sort(nearbyAnnotations); + + if (nearbyAnnotations == mAnnotationsNearLastTap) + { + // the selection candidates haven't changed; cycle through them + if (mSelectedAnnotation != null && (mSelectedAnnotation.getId() == mAnnotationsNearLastTap.get(mAnnotationsNearLastTap.size() - 1).getId())) + { + // the selected annotation is the last in the set; cycle back to the first + // note: this could be the selected annotation if only one in set + newSelectedAnnotationID = mAnnotationsNearLastTap.get(0).getId(); + } + else if (mSelectedAnnotation != null) + { + // otherwise increment the selection through the candidates + long currentID = mSelectedAnnotation.getId(); + long result = mAnnotationsNearLastTap.indexOf(mSelectedAnnotation); + newSelectedAnnotationID = mAnnotationsNearLastTap.get((int) result + 1).getId(); + } + else + { + // no current selection; select the first one + newSelectedAnnotationID = mAnnotationsNearLastTap.get(0).getId(); + } + } + else + { + // start tracking a new set of nearby annotations + mAnnotationsNearLastTap = nearbyAnnotations; + + // select the first one + newSelectedAnnotationID = mAnnotationsNearLastTap.get(0).getId(); + } + + } else { + // there are no nearby annotations; deselect if necessary + newSelectedAnnotationID = -1; + } + + if (newSelectedAnnotationID >= 0) { + + for (Annotation annotation : mAnnotations) { + if (annotation.getId() == newSelectedAnnotationID) { + if (annotation.getId() == mSelectedAnnotation.getId()) { + selectAnnotation(annotation); + } + break; + } + } + + } else { + // deselect any selected annotation + if (mSelectedAnnotation != null) { + deselectAnnotation(); + } + } + performClick(); return true; }