获取Leaflet图层控件的状态复选框

时间:2017-10-17 14:10:11

标签: javascript ajax leaflet

我有几个函数使用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();
}

1 个答案:

答案 0 :(得分:0)

通用算法可以是以下

  • 使用data-attribites

    向您的复选框添加一些元数据
    <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);   
        }
    });