javascript - 获取传单层控制复选框的状态

标签 javascript ajax leaflet

我有几个函数使用 Leaflet 在 map 上绘制不同的东西,其中一个有一个图层控件,可以显示/隐藏 map 上的一些扇区。另一个函数绘制电梯(直线)。

根据用户操作, map 上显示的内容会发生变化,我会重新绘制电梯。
我希望仅在用户选中复选框时才绘制扇区,但我不知道如何获取复选框的值并将其传递给提升功能(如果用户选中了该功能,则应触发扇区功能)复选框)。

如何保存图层控件复选框的值并在另一个 Ajax 函数(提升)中测试它?

$('#build').on("click", function(e){
    $.ajax({
        type: "POST",
        url: map_controller/show_lift_types",
        success: function(result){
            if (result.returned == true){
                // ... Displays some information in the page 
                drawLift(); // Draws the lifts
                // If the user has chosen to show the sector layer, need to call drawSectors 
                drawSectors();
            }
        }
    });
});


function drawLift() {
    if (typeof lift_path !== 'undefined') {             // If lift_path (the lifts) exists
        map.eachLayer(function (layer) {                // For each layer
            console.log(layer._leaflet_id);
            if (typeof layer._path !== 'undefined') {       // Only if the _path variable exist. Excludes the background image of the map and already built lift
            map.removeLayer(layer);                     // Remove the layer
        }
        });
    }

    $.ajax({
        type: "POST",
        dataType: "json",
        url: map_controller/get_lifts_map",
        success: function(result){
            for ( i=0; i < result.id_group.length; i++ ) {
                // ... retrieves parameters ...         
                var path_info = {
                    "type": "Feature",
                    "features": [{
                        "type": "Feature",
                        "geometry": {
                            "type": "LineString",
                            "coordinates": [start_location, end_location]
                        }
                    }]
                };
                lift_path = new L.geoJson(path_info,style: style}).on('click', function (e) {
                    // ... Some function...
                }).addTo(map);
            }
        }
    });
};

function drawSector() {
    var sector_path = new Array()   
    var baseLayers;
    $.ajax({
        type: "POST",
        dataType: "json",
        url: map_controller/get_sectors_map",
        success: function(result){  
            for ( i=0; i < result.path.length; i++ ) {
                // ... retrieves parameters ...   
                var sectors = {
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": path
                    }
                };
                sector_path[i] = L.geoJson(sectors, style);
            }
            var sectors = L.layerGroup([sector_path[0], sector_path[1], sector_path[2]]).addTo(map);
            var overlays = {};
            overlays[Settings.show_sectors] = sectors;  // Show sector checkbox
            L.control.layers(baseLayers, overlays).addTo(map);
        }
    });
}

// do the actual ajax calls
drawSector(); 
drawLift();   

更新:根据@davojta 的建议,这是我的完整解决方案:
$(document).on('change', '.leaflet-control-layers-selector', function() {
    $checkbox = $(this);
    if ($checkbox.is(':checked')) {
        sectorLayerCheckbox = true;
    }
    else {
        sectorLayerCheckbox = false;
    }
}

drawLift我补充说:
if (typeof sectorLayerCheckbox == 'undefined' || sectorLayerCheckbox != false) {  
   drawSector();
}

最佳答案

一般算法可能如下

  • 使用数据属性将一些元数据添加到您的复选框中
    <input id="checkBox" type="checkbox" data-lyftFlag="flagId">
    
  • 听更改发明并在选中复选框后采取行动
    $('#checkBox').change(function() {
        const $checkbox = $(this);
        if ($checkbox.is(':checked')) {
            const lyftFlag = $checkbox.data("lyftFlag");
            drawLift(lyftFlag);   
        }
    });
    
  • 关于javascript - 获取传单层控制复选框的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46792222/

    相关文章:

    javascript - 如何在传单杂食层上使用自定义图标?

    javascript - 如何将 d3 map 工具提示绑定(bind)到 Leaflet 的 popupPane?

    javascript - 在链接jquery mobile中传递参数

    javascript - 使用ajax post方法将变量从javascript传递到php

    javascript - 捕获 html5 地理定位事件

    javascript - 使用帖子 id 作为 id 属性来构建博客帖子

    javascript - 在 Javascript 中生成 PGP key 对,并使用加密的 PGP 私钥签署文本

    php - 我如何使用 jquery ajax 获取图像?

    javascript - 不同域中两个应用程序的浏览器之间的通信

    javascript - Bootstrap 4 传单 map 模态问题