Mapbox-gl-js: How to differentiate between clicking on map and clicking on a marker?

Created on 17 May 2015  路  8Comments  路  Source: mapbox/mapbox-gl-js

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' ?

Most helpful comment

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

All 8 comments

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

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

+1

Any update on this?

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 :)

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

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

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

Was this page helpful?
0 / 5 - 0 ratings