From dd4e5350d93240e72f96ac1dda35fede8a143714 Mon Sep 17 00:00:00 2001 From: Bastien Wermeille Date: Mon, 18 May 2020 17:21:22 +0200 Subject: [PATCH] Implement cancel draggable during onAnnotationDrag --- .../testapp/activity/annotation/CircleActivity.java | 3 ++- .../testapp/activity/annotation/SymbolActivity.java | 3 ++- .../plugins/annotation/DraggableAnnotationController.java | 7 ++++++- .../plugins/annotation/OnAnnotationDragListener.java | 3 ++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/annotation/CircleActivity.java b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/annotation/CircleActivity.java index dfd29c3f5..0442b0c1f 100644 --- a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/annotation/CircleActivity.java +++ b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/annotation/CircleActivity.java @@ -103,12 +103,13 @@ public void onAnnotationDragStarted(Circle annotation) { } @Override - public void onAnnotationDrag(Circle annotation) { + public boolean onAnnotationDrag(Circle annotation) { draggableInfoTv.setText(String.format( Locale.US, "ID: %s\nLatLng:%f, %f", annotation.getId(), annotation.getLatLng().getLatitude(), annotation.getLatLng().getLongitude())); + return true; } @Override diff --git a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/annotation/SymbolActivity.java b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/annotation/SymbolActivity.java index 6421996fc..b5b746773 100644 --- a/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/annotation/SymbolActivity.java +++ b/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/annotation/SymbolActivity.java @@ -142,12 +142,13 @@ public void onAnnotationDragStarted(Symbol annotation) { } @Override - public void onAnnotationDrag(Symbol annotation) { + public boolean onAnnotationDrag(Symbol annotation) { draggableInfoTv.setText(String.format( Locale.US, "ID: %s\nLatLng:%f, %f", annotation.getId(), annotation.getLatLng().getLatitude(), annotation.getLatLng().getLongitude())); + return true; } @Override diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/DraggableAnnotationController.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/DraggableAnnotationController.java index 153f1fd19..2aa7fbfd3 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/DraggableAnnotationController.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/DraggableAnnotationController.java @@ -101,13 +101,18 @@ boolean onMove(MoveGestureDetector detector) { ); if (shiftedGeometry != null) { + Geometry oldGeometry = draggedAnnotation.geometry; draggedAnnotation.setGeometry( shiftedGeometry ); annotationManager.internalUpdateSource(); if (!annotationManager.getDragListeners().isEmpty()) { for (D d : annotationManager.getDragListeners()) { - d.onAnnotationDrag(draggedAnnotation); + if (!d.onAnnotationDrag(draggedAnnotation)) { + draggedAnnotation.setGeometry(oldGeometry); + stopDragging(draggedAnnotation); + return true; + } } } return true; diff --git a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/OnAnnotationDragListener.java b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/OnAnnotationDragListener.java index adeed4d81..6543e1e55 100644 --- a/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/OnAnnotationDragListener.java +++ b/plugin-annotation/src/main/java/com/mapbox/mapboxsdk/plugins/annotation/OnAnnotationDragListener.java @@ -18,8 +18,9 @@ public interface OnAnnotationDragListener { * Called when an annotation dragging is in progress. * * @param annotation the annotation + * @return False if this drag should be canceled, true otherwise. */ - void onAnnotationDrag(T annotation); + boolean onAnnotationDrag(T annotation); /** * Called when an annotation dragging has finished.