从数组添加动态叠加

我正在尝试使用传单向地图添加新图层。首先,我通过表单创建图层,然后放置与其关系相关的元素。问题是将新图层添加到覆盖对象是使用静态代码,我想使用我正在创建并从数据库下载的所有图层。有没有人有想法?


var tipo1 = L.layerGroup();

var tipo2 = L.layerGroup();


var overlayers = {

  "Tipo 1": tipo1,

  "Tipo 2": tipo2

};  

这个例子有效。这是它应该如何使用。但在我的情况下,而不是使用tipo1和tipo2...我会有一个或多个来自数据库的类型,并希望添加到覆盖层。


慕尼黑的夜晚无繁华
浏览 140回答 2
2回答

红糖糍粑

您可以创建自己的 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 中并组织它们等。

慕工程0101907

您不会发布太多代码,但假设您使用的是图层控件,则可以动态添加图层 - 例如var myLayersControl = L.control.layers(null, null).addTo(map);var myNewLayer = L.layerGroup();myLayersControl.addOverlay(myNewLayer, "Description");
打开App,查看更多内容
随时随地看视频慕课网APP