Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manipulate event bubbling when registering click event upon a layer in mapbox gl js

How can I stop event propagation on layer click events?

mapBox.on('click', layerId, function (e) {
    console.log(e);
    // e.stopPropagation(); which is not working 
    // e.originalEvent.stopPropagation(); which is not working 
    var popupHtml = getPopupHtmlWrapper(e.features[0]);
    new mapboxgl.Popup({closeButton:false})
        .setLngLat(e.lngLat)
        .setHTML(popupHtml)
        .addTo(mapBox);
});
like image 286
Amit Gore Avatar asked Nov 30 '17 15:11

Amit Gore


People also ask

How do I create a new layer in Mapbox?

Click the Type option, and then choose the Symbol layer option so you can create a layer with markers.

Does Mapbox use WebGL?

Mapbox Studio is supported in browsers that support WebGL, a method of generating dynamic 3D graphics using JavaScript, accelerated through hardware. Mapbox Studio is compatible with all modern browsers, specifically: Safari 9 and above. Chrome latest.


1 Answers

Per the suggestion by @Stophface, I used

e => {
  const features = map.queryRenderedFeatures(e.point)
  if (features[0].layer.id === myLayerName ) {
    doMyClickAction(e)
  }
}

so as to only do something with the event when the layer I care about in this instance is the first one that is clicked.

like image 100
matsad Avatar answered Sep 27 '22 18:09

matsad