带有 onchange 事件侦听器的自定义传单控件 - 您是否传递图层名称或传单 ID?

发布于 2025-01-10 20:21:29 字数 3838 浏览 0 评论 0原文

在为 Leaflet 制作选项卡式控制菜单时,我将复选框的 ID 设置为组的名称,然后将该 ID 调用到一个函数中,该函数是输入的 onclick 的一部分。

当我用 me.id 调用它时,如下所示,我得到一个

 leaflet.js:2361 Uncaught Error: The provided object is not a Layer.
at i.addLayer (leaflet.js:2361:28)
at i.addLayer (leaflet.markercluster.layersupport.js:6:6275)
at RgnCtrlChk (index.html:1217:7)
at HTMLInputElement.onclick (VM18957 -1.859:1224:25)

或者类似地 Cannot create property '_leaflet_id' on string custom control based on the method I use

当我用 this._leaflet_id 调用它时,我得到

 leaflet.markercluster.layersupport.js:6 Uncaught TypeError: Cannot read properties of undefined (reading '_mcgLayerSupportGroup')
at i.addLayer (leaflet.markercluster.layersupport.js:6:6203)
at RgnCtrlChk (index.html:1217:7)
at HTMLInputElement.onclick (VM18921 -2.090:1224:25)

我不知道是否是我使用了层的名称(这不是它们在传单中的调用方式),或者我是否完全错过了它。

我的代码如下: 对于 onclick 中调用的 if/else 删除/添加图层

 function RgnCtrlChk(me) {
    if(map.hasLayer(me.id)){
    map.removeLayer(me.id);
    console.log(me.id)
    }else{
    map.addLayer(me.id);
    console.log(me.id);
    }};

,然后根据先前定义的图层名称在填充的复选框列表中创建此图层,但可能不使用 Leaflet 使用的名称?

var cats = ["Development","Regeneration","Health and Equality","Strategy","Transport","MultipleDeliverables"];
        var catslayers = ["DevelopmentGrp","RegenerationGrp","HealthAndEqualityGrp","StrategyGrp","TransportGrp","MultipledeliverablesotherGrp"]

        for (var i = 0; i < cats.length; i++) {
            var label = document.createElement('label');
                label.id = cats[i];
                tabCtrlPane2SectionDiv.appendChild(label);
                var div = document.createElement('div');
                label.appendChild(div);
                    var checkbox = document.createElement('input');
                    checkbox.id = catslayers[i];
                    checkbox.type = "checkbox";
                    checkbox.setAttribute("class","leaflet-control-layers-selector");
                    checkbox.setAttribute("onclick","return RgnCtrlChk(this)");
                    div.appendChild(checkbox);
                    var span = document.createElement('span');
                    div.appendChild(span);
                        span.appendChild(document.createTextNode(cats[i]));
                    }

有没有更简单的方法可以在不弄乱私有 _leaflet_id 的情况下做到这一点?

编辑:

然后,当首先调用图层变量时

var layer = map._layers.mcg[me.id];
        
        if(map.hasLayer(layer)){
        console.log(layer);
        map.removeLayer(layer);
        }else{
        map.addLayer(layer);
        console.log(layer);
        }};

会抛出此错误 - 我认为这是因为之前调用它的 MarkerCluster/LayerSupport 中存在分组?

leaflet.markercluster.layersupport.js:6 Uncaught TypeError: Cannot read properties of undefined (reading '_mcgLayerSupportGroup')
    at i.addLayer (leaflet.markercluster.layersupport.js:6:6203)
    at RgnCtrlChk (index.html:1220:7)
    at HTMLInputElement.onclick (VM20824 0.2204:1226:25)

检查图层

var mcg = L.markerClusterGroup.layerSupport().addTo(map),
            control2 = L.control.layers(null, null, {collapsed: false}),
            RegionCtrl = L.control.layers(null, null, {collapsed: false});

        mcg.checkIn([
            DevelopmentGrp,
            HealthAndEqualityGrp,
            RegenerationGrp,
            MultipledeliverablesotherGrp,
            TransportGrp,
            StrategyGrp
            ]);

上述代码中创建了 mcg 组并在将子组附加到 map._layers.mcg[me.id] 时 ;该组本身没有定义让我觉得我没有正确调用它?

index.html:1214 Uncaught TypeError: Cannot read properties of undefined (reading 'DevelopmentGrp')
    at RgnCtrlChk (index.html:1214:31)
    at HTMLInputElement.onclick (VM20752 0.2135:1226:25)

While making a tabbed control menu for Leaflet I've set the ID of the checkbox to the name of the group, I then call that ID into a function which is part of the onclick for an input.

When I call it with me.id, as below, I get a

 leaflet.js:2361 Uncaught Error: The provided object is not a Layer.
at i.addLayer (leaflet.js:2361:28)
at i.addLayer (leaflet.markercluster.layersupport.js:6:6275)
at RgnCtrlChk (index.html:1217:7)
at HTMLInputElement.onclick (VM18957 -1.859:1224:25)

Or similarly Cannot create property '_leaflet_id' on string custom control depending on the method I use

When I call it with this._leaflet_id I get

 leaflet.markercluster.layersupport.js:6 Uncaught TypeError: Cannot read properties of undefined (reading '_mcgLayerSupportGroup')
at i.addLayer (leaflet.markercluster.layersupport.js:6:6203)
at RgnCtrlChk (index.html:1217:7)
at HTMLInputElement.onclick (VM18921 -2.090:1224:25)

I don't know if it is through me using the names of the layers which aren't how they are called in Leaflet or if I am completely missing it.

My code is as follows:
For the if/else remove/add layer called in onclick

 function RgnCtrlChk(me) {
    if(map.hasLayer(me.id)){
    map.removeLayer(me.id);
    console.log(me.id)
    }else{
    map.addLayer(me.id);
    console.log(me.id);
    }};

and then this is created in a populated checkbox list from the names of the layers as defined previously, but maybe not using the names that Leaflet uses?

var cats = ["Development","Regeneration","Health and Equality","Strategy","Transport","MultipleDeliverables"];
        var catslayers = ["DevelopmentGrp","RegenerationGrp","HealthAndEqualityGrp","StrategyGrp","TransportGrp","MultipledeliverablesotherGrp"]

        for (var i = 0; i < cats.length; i++) {
            var label = document.createElement('label');
                label.id = cats[i];
                tabCtrlPane2SectionDiv.appendChild(label);
                var div = document.createElement('div');
                label.appendChild(div);
                    var checkbox = document.createElement('input');
                    checkbox.id = catslayers[i];
                    checkbox.type = "checkbox";
                    checkbox.setAttribute("class","leaflet-control-layers-selector");
                    checkbox.setAttribute("onclick","return RgnCtrlChk(this)");
                    div.appendChild(checkbox);
                    var span = document.createElement('span');
                    div.appendChild(span);
                        span.appendChild(document.createTextNode(cats[i]));
                    }

Is there an easier way to do this without messing with the private _leaflet_id?

EDIT:

Then, when calling the layers variable first

var layer = map._layers.mcg[me.id];
        
        if(map.hasLayer(layer)){
        console.log(layer);
        map.removeLayer(layer);
        }else{
        map.addLayer(layer);
        console.log(layer);
        }};

throws up this error - which I presumed was because due to the presence of grouping from MarkerCluster/LayerSupport where it was called earlier?

leaflet.markercluster.layersupport.js:6 Uncaught TypeError: Cannot read properties of undefined (reading '_mcgLayerSupportGroup')
    at i.addLayer (leaflet.markercluster.layersupport.js:6:6203)
    at RgnCtrlChk (index.html:1220:7)
    at HTMLInputElement.onclick (VM20824 0.2204:1226:25)

The aforementioned code where the mcg group was created and the layers are checked

var mcg = L.markerClusterGroup.layerSupport().addTo(map),
            control2 = L.control.layers(null, null, {collapsed: false}),
            RegionCtrl = L.control.layers(null, null, {collapsed: false});

        mcg.checkIn([
            DevelopmentGrp,
            HealthAndEqualityGrp,
            RegenerationGrp,
            MultipledeliverablesotherGrp,
            TransportGrp,
            StrategyGrp
            ]);

Upon appending the subgroup to map._layers.mcg[me.id]; the group itself isn't defined making me think I'm not calling it correctly?

index.html:1214 Uncaught TypeError: Cannot read properties of undefined (reading 'DevelopmentGrp')
    at RgnCtrlChk (index.html:1214:31)
    at HTMLInputElement.onclick (VM20752 0.2135:1226:25)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能否归途做我良人 2025-01-17 20:21:29

在使用 hasLayeraddLayerremoveLayer 之前,您需要先获取图层:

var layer = map._layers[YOUR_LEAFLET_ID]

该函数如下所示:

function RgnCtrlChk(me) {
    var layer = map._layers[me.id];

    if(map.hasLayer(layer)){
        map.removeLayer(layer);
        console.log(layer)
    }else{
        map.addLayer(layer);
        console.log(layer);
    }
}

但我不明白如何您可以从 html 元素 “return RgnCtrlChk(this)” 获取 leafletId。

有没有一种更简单的方法可以在不弄乱私有_leaflet_id的情况下做到这一点?

Leaflet id 的公共函数是 L.stamp(object)

You need first to get the layer befor you can use hasLayer, addLayer and removeLayer:

var layer = map._layers[YOUR_LEAFLET_ID]

The function would look like:

function RgnCtrlChk(me) {
    var layer = map._layers[me.id];

    if(map.hasLayer(layer)){
        map.removeLayer(layer);
        console.log(layer)
    }else{
        map.addLayer(layer);
        console.log(layer);
    }
}

But what I not understand how you get the leafletId from the html element "return RgnCtrlChk(this)".

Is there an easier way to do this without messing with the private _leaflet_id?

The public function for the Leaflet id is L.stamp(object)

玩物 2025-01-17 20:21:29

没有通过 JavaScript 发送单击的复选框,而是采用 jQuery 方法,效果很好。

$(".p2input").change(function(){
        var layerClicked = window[$(this).attr("id")];
            console.log(layerClicked);
        if(map.hasLayer(layerClicked)){
            map.removeLayer(layerClicked);
        }else{
            map.addLayer(layerClicked);
          }});

Upon instead of sending the clicked checkbox via JavaScript taking a jQuery approach worked well.

$(".p2input").change(function(){
        var layerClicked = window[$(this).attr("id")];
            console.log(layerClicked);
        if(map.hasLayer(layerClicked)){
            map.removeLayer(layerClicked);
        }else{
            map.addLayer(layerClicked);
          }});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文