简体   繁体   English

传单:单击叠加的geojson图层时,地图点击事件不起作用

[英]Leaflet: Map Click Event doesn't work when clicked on an overlay geojson layer

I'm using Leaflet in one of my projects and I'm facing an issue, which is explained below: 我在其中一个项目中使用Leaflet,但遇到一个问题,下面对此进行了说明:

I've implemented a click event on the map using 我已使用以下方法在地图上实现了click事件

map.on('click', function(e) {        
    console.log(e)       
});

and everything is working fine. 而且一切正常。

I've also added a geojson layer on map. 我还在地图上添加了geojson图层。 So, the problem is when I click on the overlay geojson layer (which is a polygon layer actually), the map click event not fired. 因此,问题是当我单击覆盖的geojson图层(实际上是多边形图层)时,未触发地图单击事件。 Hence, my question is what should I do so that map click event also work when I click on an overlay layer on map? 因此,我的问题是,当我单击地图上的叠加层时,如何使地图单击事件也起作用?

As it's being said in the comments the click event is binded to the map or to the overlay layers but it's not bubbled from one to another. 就像在评论中所说的那样, click事件绑定到了地图或叠加层,但没有从另一个冒泡。 You must attach it to the desired object. 您必须将其附加到所需的对象。

You can bind the click to the whole overlay as it inherits from L.Evented or to individual features. 您可以将点击绑定到从L.Evented继承的整个叠加层,也可以绑定到各个要素

To bind it to individual features check the onEachFeature section of the tutorial . 要将其绑定到各个功能,请查看本教程onEachFeature部分

function onEnachFeature(feature, layer) {
    console.log(feature.properties);
}
L.geoJSON(geojsonFeature, {
    onEachFeature: onEachFeature
}).addTo(map);

Depending on what you are trying to achieve the the bindPopup and the bindTooltip will be useful. 根据您要实现的目标, bindPopupbindTooltip会很有用。

Also, take into account that as the doc explains 另外,请考虑到文档所说明的

Events are propagated to the FeatureGroup, so if the group has an event handler, it will handle events from any of the layers. 事件会传播到FeatureGroup,因此,如果该组具有事件处理程序,它将处理来自任何图层的事件。 This includes mouse events and custom events. 这包括鼠标事件和自定义事件。

So you can choose to bind to each feature or to the layer with similar effects depending on your use case. 因此,根据您的用例,您可以选择绑定到每个要素或具有相似效果的图层。

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

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