【问题标题】:How to capture a click on a specific leaflet layer如何捕获对特定传单图层的点击
【发布时间】:2020-05-14 06:24:29
【问题描述】:

我认为这个问题说明了一切。 我正在使用传单。我正在将三层加载到地图上。

但是,在单击地图后,我找不到知道单击了哪个图层的方法。 基本上是因为没有在图层上设置事件处理程序,只在地图上设置。

我还尝试将图层添加到功能组中,并向该功能组添加点击事件。然而,点击地图不会导致任何事件/响应。

这是我在 featureGroup 中所做的:

addWaterNameLayers: function() {

    var knownWaters = L.tileLayer.wms(getGeoServer('wms', geoEnviroment), {
        layers: this.wmsLayers.known.name,
        format: 'image/png',
        opacity: 0,
        styles: 'cursor: pointer',
        transparent: true,
        attribution: ""
    });//.addTo(this.mapInfo);

    var unknownWaters = L.tileLayer.wms(getGeoServer('wms', geoEnviroment), {
        layers: this.wmsLayers.unknown.name,
        format: 'image/png',
        opacity: 0.3,
        styles: '',
        transparent: true,
        attribution: ""
    });

    L.FeatureGroup(knownWaters, unknownWaters).on('click', function(event) {
       console.log('click');
       this.handleClick(event);
    },this);

    //this part will work on mapclick, so on featuregroup it should work?
    //when clicking on the map
    /*
    this.mapInfo.on('click', function(event) {
        this.handleClick(event);
    }, this);
    */

},

在上面的代码中,您还可以在地图上看到 click 事件……这很有效。 在功能组上它没有。

此外,当我将 featureGroup 的代码更改为这些代码时,它也不起作用:

    var featGr = L.FeatureGroup(knownWaters, unknownWaters).on('click', function(event) {
       this.handleClick(event);
    },this);


    var featGr = L.FeatureGroup(knownWaters, unknownWaters);
    featGr.on('click', function(event) {
       this.handleClick(event);
    },this);

将featureGroup添加到图层也不会做任何事情......

【问题讨论】:

  • 您找到解决方案了吗?我也有同样的问题...
  • 需要迭代 WMS 层,但已经有一段时间了,所以不记得了。

标签: javascript leaflet


【解决方案1】:

将您的图层放入L.FeatureGroup。 L.FeatureGroup 是 L.LayerGroup 的扩展,它添加了事件。它还支持 clickevent,所以这正是您正在寻找的。查看文档:http://leafletjs.com/reference.html#featuregroup

【讨论】:

  • thnx 但我确实把它放在了一个功能组中,但是我无法捕获点击。为了确定,我会再试一次。
  • 我没有意识到你在谈论 (wms) tilelayers。这当然永远不会与 tilelayers 一起使用,因为它们是重叠的图像。除了当前位于顶部的可见层之外,您永远不会得到另一个结果。
  • 我很害怕 ;) 所以除了迭代层之外别无他法?
【解决方案2】:

event.layer是图层数据

event.originalEvent.path[0]

是你点击的元素

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多