Skip to content

Commit

Permalink
mapbox#894 - Moving annotation selection determination to onSingleTap…
Browse files Browse the repository at this point in the history
…Up() 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
  • Loading branch information
bleege authored and incanus committed Sep 7, 2015
1 parent c5d3a1c commit 0e2507a
Showing 1 changed file with 80 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -973,86 +973,6 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
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<Annotation> 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();
Expand Down Expand Up @@ -1111,6 +1031,86 @@ public boolean onDoubleTap(MotionEvent e) {
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<Annotation> 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;
}
Expand Down

0 comments on commit 0e2507a

Please sign in to comment.