红糖糍粑
您可以创建自己的 featureGroup 类 - 例如: L.Overlayers = L.FeatureGroup.extend({ initialize: function(options) { L.setOptions(this, options); L.FeatureGroup.prototype.initialize.call(this, options); }, onAdd: function(map) { L.FeatureGroup.prototype.onAdd.call(this, map); this._map = map; this.on("layeradd", this._doThingsHere, this); this.on("layerremove", this._stopDoingThingsHere, this); }, onRemove: function() { this.off("layeradd", this._doThingsHere, this); this.off("layerremove", this._stopDoingThingsHere, this); }, });如果在地图上实例化它的一个空实例:var group = new L.Overlayers().addTo(map);然后当您的数据层加载时,您可以将其动态添加到组中:group.addLayer(tipo1);这将触发layeradd并让您通过e.layer从那里您可以完全访问图层和地图。你可以做一些事情,比如将它们分组到 LayerGroups 中并组织它们等。