mapbox-gl-native-android icon indicating copy to clipboard operation
mapbox-gl-native-android copied to clipboard

Order of events' management in `OnTouchEven`

Open Ph0tonic opened this issue 3 years ago • 2 comments

Hello,

There is an issue in mapbox-annotation-plugin https://github.com/mapbox/mapbox-plugins-android/issues/1162 which is link on the order that events are handle. When we drag a symbol and we have also a long click listener on the map then we first trigger the event in mapView and after that the event added via addTouchListener triggers the drag event.

One solution to this problem would be to first trigger the added listener and then the mapview ones. So if the drag event is triggered it will consume the event and no long click will be triggered.

So instead of having :

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (isGestureDetectorInitialized() && mapGestureDetector.onTouchEvent(event) || super.onTouchEvent(event)) {
      return true;
    }

    for (OnTouchListener listener : onTouchListeners) {
      if (listener.onTouch(this, event) {
        return true;
      }
    }
    return false;
  }

We would have

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    for (OnTouchListener listener : onTouchListeners) {
      if (listener.onTouch(this, event) {
        return true;
      }
    }
    return isGestureDetectorInitialized() && mapGestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
  }

What do you think, let me know and then I will open a PR. Thanks @mapbox/maps-android

\cc @samcrawford

Ph0tonic avatar Feb 27 '21 17:02 Ph0tonic

Thanks, @Ph0tonic. We need mapGestureDetector to handle the map movement events, so if there is a registered OnTouchListener and consumes the touch event, we will not be able to move the map.

Chaoba avatar Mar 01 '21 00:03 Chaoba

Hello, Sorry for the late answer @Chaoba but this is the point. We should be able to handle events before any map update. For example with the annotation plugin, we want to handle the drag event of an annotation and consume it before the map. Because it would means that the map will be moved and the event consumed. Maybe I'm missing a requirement of the architecture but I think that for the annotation plugin it's required.

Ph0tonic avatar Jun 25 '21 09:06 Ph0tonic