Leaflet图层与地图点击时的不同操作

huangapple go评论59阅读模式
英文:

Different actions for clicking on Leaflet layer vs map

问题

以下是翻译好的部分:

"I've got a click event for the main part of my leaflet map. But if the user clicks on a specific layer I want to do something else and not the other action. I thought I could stop the propagation of the map click using this answer

map.originalEvent.preventDefault()

But I still get both events.

Here's a cutting from my code

this.map.on('click', (event) => {
console.log('map clicked')
})

this.boundaryLayer = L.geoJSON().addTo(this.map);
this.boundaryLayer.on('click', (event) => {
console.log('boundary clicked', event);
event.originalEvent.preventDefault();
});

英文:

I've got a click event for the main part of my leaflet map. But if the user clicks on a specific layer I want to do something else and not the other action. I thought I could stop the propagation of the map click using this answer

> map.originalEvent.preventDefault()

But I still get both events.

Here's a cutting from my code

    this.map.on('click', (event) => {
        console.log('map clicked')
    })


    this.boundaryLayer = L.geoJSON().addTo(this.map);
    this.boundaryLayer.on('click', (event) => {
        console.log('boundary clicked', event);
        event.originalEvent.preventDefault();
    });

答案1

得分: 0

这段代码阻止了地图的点击事件被触发:

this.boundaryLayer.on('click', (event) => {
    console.log('boundary clicked', event);
    L.DomEvent.stopPropagation(event);
});
英文:

This code stops the map's click event from being fired

    this.boundaryLayer.on('click', (event) => {
            console.log('boundary clicked', event);
            L.DomEvent.stopPropagation(event);
    });

huangapple
  • 本文由 发表于 2023年6月29日 08:02:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定