Mapbox-gl-js: The popup is not triggered when click on the html marker

Created on 14 Sep 2017  路  1Comment  路  Source: mapbox/mapbox-gl-js

Mapbox gl js Version 39/40

I am not sure, but when I click on the html Marker I see the click event in the console,
but there is no popup window. But when I click and hold the mouse button and move the mouse and then release the mouse button the popup appeares.

map.on('load', function() {

  var el = document.createElement('div');
  el.innerHTML = "CLICK ME"
  el.style.backgroundColor = "red"
  el.addEventListener('click', function(e) {
    console.log("click", e);
    new mapboxgl.Popup()
      .setLngLat([-77.04, 38.907])
      .setHTML("divInfo")
      .addTo(map);
  })
  new mapboxgl.Marker(el)
    .setLngLat([-77.04, 38.907])
    .addTo(map);
});

Here is a jsfiddle https://jsfiddle.net/1gwx64sd/

Most helpful comment

What actually happens is that the click triggers the popup opening, but then the event propagates through marker parents in the DOM, including the map container, which triggers a "click to close an open popup" action. To fix this, I added e.stopPropagation() to the event handler: https://jsfiddle.net/1gwx64sd/1/

>All comments

What actually happens is that the click triggers the popup opening, but then the event propagates through marker parents in the DOM, including the map container, which triggers a "click to close an open popup" action. To fix this, I added e.stopPropagation() to the event handler: https://jsfiddle.net/1gwx64sd/1/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aderaaij picture aderaaij  路  3Comments

rasagy picture rasagy  路  3Comments

samanpwbb picture samanpwbb  路  3Comments

PBrockmann picture PBrockmann  路  3Comments

aendrew picture aendrew  路  3Comments