单击标记单击其下方的图层(Mapbox-gl)

时间:2018-10-17 03:56:34

标签: mapbox mapbox-gl

当我单击地图上的标记时,单击也将由标记下方运行的路径的onclick侦听器记录。我该如何阻止这种情况的发生?我希望点击仅由标记的onclick侦听器注册。

Marker on map

标记的代码:

var el = document.createElement('div');
    el.className = 'marker';
    el.addEventListener('click', () => {
        //functions
    })

    const marker = new mapboxgl.Marker(el)
      .setLngLat([location.longitude, location.latitude])
      .addTo(this.map);

该层的代码:

map.addLayer({
        "id": "route",
        "type": "line",
        "source": {
          "type": "geojson",
          "data": path
        },
        "layout": {
          "line-join": "round",
          "line-cap": "round"
        },
        "paint": {
          "line-color": "#888",
          "line-width": 8
        }
      });

1 个答案:

答案 0 :(得分:1)

尝试捕获事件并停止传播:

el.addEventListener('click', e => {
    e.stopPropagation();
    //functions
}, true);