如何启用/禁用函数的结果

How to enable/disable the result of a function

本文关键字:函数 结果 何启用 启用      更新时间:2023-09-26

我有一个函数,可以从文件中读取我的标记并将它们输出到我的地图中。这是我的函数:

function showResourcesByName(name) {
                for (var i = 0; i < markers.resources.length; i++) {
                    var resName = markers.resources[i].name;
                    if (resName == name) {
                        var resIcon = icons.resources[i].icon;
                        var resSize = icons.resources[i].size;
                        var resPname = icons.resources[i].pname;
                        var customIcon = L.icon({
                            iconUrl: resIcon,
                            iconSize: resSize, // size of the icon
                            iconAnchor:   [resSize[0]/2, resSize[1]/2], // point of the icon which will correspond to marker's location
                            popupAnchor:  [2, -resSize[1]/2] // point from which the popup should open relative to the iconAnchor
                        });
                        for (var j = 0; j < markers.resources[i].coords.length; j++) {
                            var x = markers.resources[i].coords[j].x;
                            var y = markers.resources[i].coords[j].y;
                            marker = L.marker([y, x], {icon: customIcon});
                            marker.addTo(map).bindPopup(resPname);
                            $(marker._icon).addClass('chutiya')

                        }
                    }
                }
            }

为了显示我的标记,我使用这样的函数:

showResourcesByName("AITokarServer");
showResourcesByName("AIBorServer");

如果我这样评论结果://showResourcesByName("AITokarServer");标记消失。

我的问题是我有来自 5950 组的 29 个标记,一旦页面加载,所有这些标记都会加载。

我想创建一个复选框来启用/禁用该功能的结果,就像我可以通过评论来启用和禁用它一样。可能吗?

我的临时解决方法是使用 css 类打开/关闭标记(每个标记都有一个带有其组名的类(:

$('#shigitoggle').change(function () {
    if (!this.checked) 
    //  ^
$('.AIShigiServer').fadeOut('slow');
    else 
$('.AIShigiServer').fadeIn('slow');

}(;

问题是当它们"关闭"时,它们仍在代码中,使页面变慢(我这么说是因为代码中有 5950 个标记(。

你可以在这里看到我的完整代码:http://plnkr.co/edit/s5xUx9LQcwYP3g3Cx7MX?p=preview

我的问题是我有来自 5950 组的 29 个标记,一旦页面加载,所有这些标记都会加载。

使用 Leaflet.markercluster 不会一次显示所有标记。

问题是当它们"关闭"时,它们仍在代码中,使页面变慢(我这么说是因为代码中有 5950 个标记(。

不。在数据结构中有 6000 个项目并浪费一点内存不是问题。在您的网页中有 6000 个可见的 DOM 元素是问题所在。