Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent draggable marker touch event propagation #9755

Open
MarcL opened this issue Jun 3, 2020 · 5 comments
Open

Prevent draggable marker touch event propagation #9755

MarcL opened this issue Jun 3, 2020 · 5 comments
Labels
needs investigation 🔍 Issues that require further research (e.g. it's not clear whether it's GL JS or something else)

Comments

@MarcL
Copy link

MarcL commented Jun 3, 2020

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

@mourner
Copy link
Member

mourner commented Jun 4, 2020

Try this:

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

@MarcL
Copy link
Author

MarcL commented Jun 4, 2020

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 MarcL closed this as completed Jun 4, 2020
@MarcL
Copy link
Author

MarcL commented Jun 14, 2020

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 MarcL reopened this Jun 14, 2020
@MarcL
Copy link
Author

MarcL commented Jun 24, 2020

Hi @mourner.

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

Thanks,
Marc

@mourner mourner added the needs investigation 🔍 Issues that require further research (e.g. it's not clear whether it's GL JS or something else) label Nov 9, 2020
@paul23093
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation 🔍 Issues that require further research (e.g. it's not clear whether it's GL JS or something else)
Projects
None yet
Development

No branches or pull requests

3 participants