diff --git a/plugin-annotation/src/main/java/org/maplibre/android/plugins/annotation/AnnotationManager.java b/plugin-annotation/src/main/java/org/maplibre/android/plugins/annotation/AnnotationManager.java index 7350f2d4..445ef34d 100644 --- a/plugin-annotation/src/main/java/org/maplibre/android/plugins/annotation/AnnotationManager.java +++ b/plugin-annotation/src/main/java/org/maplibre/android/plugins/annotation/AnnotationManager.java @@ -244,6 +244,8 @@ public void updateSource() { mapView.post(() -> { isSourceUpToDate.set(true); + enableDragIfNeeded(); + if (!style.isFullyLoaded()) { // We are in progress of loading a new style return; @@ -398,6 +400,19 @@ private void initializeSourcesAndLayers(GeoJsonOptions geoJsonOptions) { updateSource(); } + private void enableDragIfNeeded(){ + boolean hasDraggableAnnotation = false; + for (int i = 0, size = annotations.size(); i < size; i++) { + Annotation annotation = annotations.valueAt(i); + + if (annotation.isDraggable()) { + hasDraggableAnnotation=true; + break; + } + } + draggableAnnotationController.setEnable(hasDraggableAnnotation); + } + /** * Inner class for transforming map click events into annotation clicks */ diff --git a/plugin-annotation/src/main/java/org/maplibre/android/plugins/annotation/DraggableAnnotationController.java b/plugin-annotation/src/main/java/org/maplibre/android/plugins/annotation/DraggableAnnotationController.java index 97ba3e17..fcd3d92d 100644 --- a/plugin-annotation/src/main/java/org/maplibre/android/plugins/annotation/DraggableAnnotationController.java +++ b/plugin-annotation/src/main/java/org/maplibre/android/plugins/annotation/DraggableAnnotationController.java @@ -2,12 +2,12 @@ import android.annotation.SuppressLint; import android.graphics.PointF; -import android.view.MotionEvent; -import android.view.View; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.UiThread; import androidx.annotation.VisibleForTesting; + import org.maplibre.android.gestures.AndroidGesturesManager; import org.maplibre.android.gestures.MoveDistancesObject; import org.maplibre.android.gestures.MoveGestureDetector; @@ -54,6 +54,8 @@ private static void clearInstance() { @Nullable private AnnotationManager draggedAnnotationManager; + private AndroidGesturesManager androidGesturesManager; + @SuppressLint("ClickableViewAccessibility") DraggableAnnotationController(MapView mapView, MapLibreMap maplibreMap) { this(mapView, maplibreMap, new AndroidGesturesManager(mapView.getContext(), false), @@ -72,18 +74,25 @@ public DraggableAnnotationController(MapView mapView, MapLibreMap maplibreMap, this.touchAreaMaxX = touchAreaMaxX; this.touchAreaMaxY = touchAreaMaxY; - androidGesturesManager.setMoveGestureListener(new AnnotationMoveGestureListener()); + this.androidGesturesManager = androidGesturesManager; + } + + void setEnable(boolean enable) { + if (enable) { + androidGesturesManager.setMoveGestureListener(new AnnotationMoveGestureListener()); - mapView.setOnTouchListener(new View.OnTouchListener() { - @Override - public boolean onTouch(View v, MotionEvent event) { + mapView.setOnTouchListener((v, event) -> { // Using active gesture manager Annotation oldAnnotation = draggedAnnotation; androidGesturesManager.onTouchEvent(event); // if drag is started or drag is finished, don't pass motion events further - return draggedAnnotation != null || oldAnnotation != null; - } - }); + boolean t = draggedAnnotation != null || oldAnnotation != null; + return t; + }); + } else { + androidGesturesManager.removeMoveGestureListener(); + mapView.setOnTouchListener(null); + } } void addAnnotationManager(AnnotationManager annotationManager) {