传单问题:使折线的图层组不可单击

Leaflet issue: making layergroup of polylines not clickable

本文关键字:图层 单击 折线 单问题 问题      更新时间:2023-09-26

我在图层组中使折线不可单击时遇到问题...正如您在下面的代码中看到的那样,我使用传单的内置函数来浏览图层组(route.eachLayer),并尝试添加样式"可点击:false",但这似乎没有任何作用......

//route = layergroup with all polylines
function disableclicking(){
    route.eachLayer(function(layer){
        layer.setStyle({clickable: false});
    });
}

当我尝试使用此代码时,它似乎没有任何变化(尽管它确实进入了循环)或者至少,它不会改变我想要的...

我想要的是删除折线的类".leaflet-clickable"......而这似乎没有发生。当您将样式更改为不可单击时,此类不会更改还是我的循环有问题?

我也有类似的需求,并且由于接受的答案与问题无关,我将发布我想出的解决方案(以防其他人从谷歌到达这里)。

function setClickable(target, value) {
    if(value && !target.options.clickable) {
        target.options.clickable = true;
        L.Path.prototype._initEvents.call(target);
        target._path.removeAttribute('pointer-events');
    } else if(!value && target.options.clickable) {
        target.options.clickable = false;
        // undoing actions done in L.Path.prototype._initEvents
        L.DomUtil.removeClass(target._path, 'leaflet-clickable');
        L.DomEvent.off(target._container, 'click', target._onMouseClick);
        ['dblclick', 'mousedown', 'mouseover', 'mouseout', 'mousemove', 'contextmenu'].forEach(function(evt) {
            L.DomEvent.off(target._container, evt, target._fireMouseEvent);
        });
        target._path.setAttribute('pointer-events', target.options.pointerEvents || 'none');
    }
}
setClickable(myLayer, false);