简体   繁体   English

单击图层时如何阻止地图单击事件触发?

[英]How can I stop a map click event from firing when clicking on a layer?

I have a click event that fires when clicking on the map.我有一个点击地图时触发的点击事件。 However, I also have a separate event when clicking on a marker layer.但是,单击标记层时,我也有一个单独的事件。 Is it possible to stop the map click event from firing when I click on a marker?当我单击标记时,是否可以停止触发地图单击事件?

The map click event:地图点击事件:

map.on('click', function (e) {
    if (isAddingLocations) {
        console.log(e.lngLat);
        $('#formtable').modal('show');
        clickCoords = e.lngLat.toArray();
        document.getElementById("latitudef").value = String(clickCoords[1])
        document.getElementById("longitudef").value = String(clickCoords[0]);
    }
});

The marker click event:标记点击事件:

map.on("click", "quebec", function (e) {
    map.getCanvas().style.cursor = 'pointer';
    var location = e.features[0].properties.location;
    var province = e.features[0].properties.province;
    var link = e.features[0].properties.link;
    var coordinates = e.features[0].geometry.coordinates.slice();

    timeadded = e.features[0].properties.timeadded;

    document.getElementById("location").innerHTML = location + ", " + province;
    document.getElementById("link").innerHTML = '<a class="btn btn-info btn-lg" href="' + link + '"target="_blank" role="button">More Info</a>';
    document.getElementById("latitudee").innerHTML = coordinates[1];
    document.getElementById("longitudee").innerHTML = coordinates[0];
    document.getElementById("locatione").innerHTML = location;
    document.getElementById("provincee").innerHTML = province;
    document.getElementById("linke").innerHTML = link;
            
    if (isAddingLocations) {
        $('#formedit').modal('show');
    }
    else {
        sidedivsize_toggle(sdiv3, sdiv1, sdiv2);
    }
});

I was thinking maybe there could be a "not 'quebec'" expression or something in the layer parameter.我在想也许在 layer 参数中可能有一个“not 'quebec'”表达式或其他东西。 Or perhaps a if the second event fires prevent the first one.或者,如果第二个事件触发阻止了第一个事件。 Not sure how one could do any of those things.不知道如何做任何这些事情。 And of course, any other solution is welcome.当然,欢迎任何其他解决方案。

There are probably various solutions to this issue.这个问题可能有多种解决方案。 One solution is the clickOneLayer function of Map-GL-Utils which fires different callbacks depending on whether a layer was clicked or whether a click missed everything.一种解决方案是 Map-GL-Utils 的clickOneLayer函数,它根据是否单击图层或单击是否错过所有内容来触发不同的回调。

Found a work around in MB github if anyone else comes across this.如果其他人遇到此问题,请在 MB github 中找到解决方法。 https://github.com/mapbox/mapbox-gl-js/issues/9875 https://github.com/mapbox/mapbox-gl-js/issues/9875

Essentially though, we stop the click event from doing anything as it bubbles up the dom chain from the Map Layer, to the Map.但本质上,我们阻止 click 事件做任何事情,因为它从 Map Layer 到 Map 的 dom 链冒泡。 Example :例子 :

map.on('click', 'points', function (e) {
    console.log('clicked on layer', e);
    e.clickOnLayer = true;
});
                
map.on('click', function (e) {
    if (e.clickOnLayer) {
        return;
    }
    console.log('map clicked', e);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM