【问题标题】:get status of Leaflet layer control checkbox获取 Leaflet 图层控制复选框的状态
【发布时间】:2017-10-17 14:10:11
【问题描述】:

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

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

如何保存层控制复选框的值并在另一个 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();
}

【问题讨论】:

标签: javascript ajax leaflet


【解决方案1】:

一般算法如下

  • 使用数据属性将一些元数据添加到您的复选框

    <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);   
        }
    });
    

【讨论】:

  • 我设法用$(document).on('change', '.leaflet-control-layers-selector', function() { 捕捉到点击,然后用const lyftFlag = true/false; 存储值
  • @remyremy 能否提供 jsfiddle 来查看和调试?
  • 我用最终解决方案编辑了我的问题。感谢您的帮助,它现在可以正常工作了!
猜你喜欢
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多