简体   繁体   English

如何将单击事件绑定到Leaflet画布GridLayer

[英]How to bind a click event to a Leaflet canvas GridLayer

I'm trying to bind a click event to a leaflet plug-in's extension of a canvas GridLayer (using Leaflet 1.0, with Leaflet.MaskCanvas ). 我正在尝试将click事件绑定到leaflet插件的画布GridLayer的扩展(使用Leaflet 1.0,使用Leaflet.MaskCanvas )。

From the Leaflet Documentation about GridLayer , I would expect that I could bind a click to coverageLayer using either 从关于GridLayer的Leaflet文档中 ,我希望我可以使用其中任何一个将点击绑定到coverageLayer

// add event listener to determine when layer has been clicked
coverageLayer.on('click', function(e) {
  console.log('clicked the line');
});
// second (alias) method to add event listener
coverageLayer.addEventListener('click', function(e) {
  console.log('clicked the line');
});

but neither of the above seem to be working. 但上述情况似乎都没有奏效。

Here's a fiddle where I've forked and tweaked the example code from the MaskCanvas plugin. 这是一个小提琴 ,我从MaskCanvas插件中分叉和调整了示例代码。

Is there some other way to bind a click to a canvas layer in Leaflet? 是否有其他方法将单击绑定到Leaflet中的画布层?

Edit: .on() and .addEventListener() are aliases. 编辑: .on().on() .addEventListener()是别名。

Previously the question dealt with Leaflet 0.7, and TileLayer. 以前这个问题涉及Leaflet 0.7和TileLayer。 From the old Leaflet Documentation , it appears that TileLayer did not have these events. 旧的Leaflet文档中可以看出,TileLayer没有这些事件。 JSFiddle code has been updated to use Leaflet 1.0. JSFiddle代码已更新为使用Leaflet 1.0。

In Leaflet 1.0, GridLayer s do not handle mouse/touch/pointer events (notice their events documentation does not list pointer events, whereas some other layer types do ). 在Leaflet 1.0中, GridLayer不处理鼠标/触摸/指针事件(注意它们的事件文档 没有列出指针事件,而其他一些图层类型也是如此 )。

Furthermore, the DOM element containing the tiles (a <div class='leaflet-tile-container> ) has a pointer-events: none; 此外,包含tile的DOM元素( <div class='leaflet-tile-container> )有一个pointer-events: none; CSS rule. CSS规则。 This makes the browser ignore pointer events (giving it a tiny bit of better performance). 这使浏览器忽略指针事件(给它一点点更好的性能)。

You'll need to attach the events to the cells themselves (when on your custom createTile() ) and override the pointer-events CSS rule. 您需要将事件附加到单元格本身(在自定义createTile() )并覆盖pointer-events CSS规则。

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

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