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

How to differentiate between clicking on map and clicking on a marker? #1209

Closed
misterfresh opened this issue May 17, 2015 · 11 comments
Closed

Comments

@misterfresh
Copy link

I'd like to have a popup open when clicking on a marker (with info specific to that marker).
I see that the popup example displays a popup when clicking on the map.
How to differentiate between the two events "clicking on the map" and "clicking on a marker' ?

@tmcw
Copy link
Contributor

tmcw commented May 26, 2015

Currently not - supporting this kind of behavior is part of the previous ticket #656

@tmcw tmcw closed this as completed May 26, 2015
@3zzy
Copy link

3zzy commented Jun 12, 2018

Was this feature added? I could find examples where clicking on 'symbol' and 'cluster' worked but still unsure how to detect click on markers.

@armved
Copy link

armved commented Jun 18, 2018

+1

@AshwinTayson
Copy link

Any update on this?

@asheemmamoowala
Copy link
Contributor

@Alex-Horlock
Copy link

Alex-Horlock commented Dec 21, 2019

For those of you who get here wondering how on earth Mapbox GL JS (which seems to be awesome) could not implement a 'click' event like I was... turns out the solution is actually pretty nice and you just need to get the markers 'element' and listen to that click event e.g.

marker.getElement().addEventListener('click', function (e) { console.log("marker clicked"); });

Cheers :)

@renatoniola
Copy link

For those of you who get here wondering how on earth Mapbox GL JS (which seems to be awesome) could not implement a 'click' event like I was... turns out the solution is actually pretty nice and you just need to get the markers 'element' and listen to that click event e.g.

marker.getElement().addEventListener('click', function (e) { console.log("marker clicked"); });

Cheers :)

My concern about your solution is that it's going to apply one event handler for each marker and if there are lots of them the page might become too slow

@ibushah
Copy link

ibushah commented Mar 19, 2020

marker.getElement().addEventListener('click', function (e) { console.log("marker clicked"); });

u just saved me from depression, love you man <3 <3 <3

@eusebiu
Copy link

eusebiu commented Dec 16, 2021

                    var el = document.createElement('div');
                    el.innerHTML = `<label>some text</label>`;

                    const marker = new mapboxgl.Marker()
                      .setLngLat({ lng: longitude, lat: latitude })
                      .setPopup(new mapboxgl.Popup().setDOMContent(el))
                      .addTo(that.map);

                    marker.getElement().addEventListener('click', (e) => { marker.togglePopup(); e.stopPropagation(); }, false);

@crhistianramirez
Copy link

My use case is different in that I want to know when the map is clicked and the click was not on a marker. Anybody have an idea on how to differentiate?

@expdev
Copy link

expdev commented Apr 8, 2022

My use case is different in that I want to know when the map is clicked and the click was not on a marker. Anybody have an idea on how to differentiate?

for example, you could differentiate a click on a marker from a click on a map, check the property 'map.getCanvas().style.cursor'. If it's "pointer", then it's a marker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests