mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

Prevent draggable marker touch event propagation

Open MarcL opened this issue 5 years ago • 6 comments

mapbox-gl-js version: v1.9.0

Question

I'm using Mapbox in a Facebook Messenger chatbot which renders the map inside an HTML webview. When dragging a draggable marker, the touchmove event also gets propagated to the parent webview. This then moves it up and down. I can't immediately see a way to get the event in the drag handlers so that I could call event. stopPropagation().

Here's an example of the issue:

mapbox-messenger

Is there an accepted method to achieve this? Can I get access to the events in the drag handlers or is there a better way to do this?

Thanks!

Links to related documentation

https://docs.mapbox.com/mapbox-gl-js/api/markers/#marker-events

MarcL avatar Jun 03 '20 20:06 MarcL

Try this:

map.on('touchmove', (e) => e.originalEvent.stopPropagation());

mourner avatar Jun 04 '20 12:06 mourner

Hi @mourner.

Thanks for the reply. That's perfect - thank you. I didn't know about originalEvent so I was attempting to do e.stopPropagation(). I also had to focus the map too as I have an input on the screen.

Thanks, Marc

MarcL avatar Jun 04 '20 18:06 MarcL

Hi @mourner! 👋

Sorry to re-open this issue. I've just got back to working on the Messenger webview and your solution above only solves half of the problem.

It now avoids propagating the touchmove event in the map and that works great. However, when you have a draggable marker, it moves the webview when you move that around as shown in the gif below:

mapbox-webview-touch

I thought I might be able to stop event propagation on the drag event for a Marker but it doesn't appear to give me the event.originalEvent in the same way as the Map handler does.

Some example code I tried but it doesn't work as originalEvent is undefined:

const marker = new mapboxgl.Marker({
        draggable
    })
    .setLngLat([longitude, latitude])
    .addTo(map);

// Complains that "event.originalEvent" is undefined
marker.on('drag', (event) => event.originalEvent.stopPropagation();

I'm sure it's something obvious again but is there a way to allow dynamic markers but avoid the event propagation to the parent webview again?

Thanks!

MarcL avatar Jun 14 '20 19:06 MarcL

Hi @mourner.

Sorry to tag you again. Have you got any ideas about the above question?

Thanks, Marc

MarcL avatar Jun 24 '20 14:06 MarcL

Hi guys, did you manage to fix the issue above? +1 for me, trying to use mapbox to drag custom polygons in Telegram webapp bot and got the same behaviour.

paul23093 avatar Jun 17 '24 08:06 paul23093

I'm also trying to do a stopPropagation on the drag events of a marker without success

paulomesquita-wsn avatar May 15 '25 20:05 paulomesquita-wsn