如何解决如何在ol6中动态添加多个矢量层到图层组?
我想在单击按钮时将图层动态添加到图层组。 我添加了一个OSM层
var map = new Map({
target: 'map',layers: [
new TileLayer({
title: 'OSM',source: new OSM(),opacity: 0.5,})
]
});
//--On button click--
var yearcmpGrp = new LayerGroup({
title: 'Year Comparison',layers: []
});
map.addLayer(yearcmpGrp); //this add a new layergroup
for(var i=fromyr;i<=toyear;i++){
var sampledata = data;
var samplevectorlyr = new VectorLayer({
title:i,source: new VectorSource({
features: new GeoJSON().readFeatures(sampledata,{
dataProjection: 'EPSG:32643',featureProjection: 'EPSG:32643',}),style: new Style({
image: new Circle({
radius: 7,fill: new Fill({color: colorpick[i]}),stroke: new Stroke({
color: [255,0],width: 2
})
})
}),});
//map.addLayer(samplevectorlyr); //this works fine & add a new layer outside layer group
map.getLayerGroup(yearcmpGrp).addLayer(samplevectorlyr); //This don't work !!
}
我想在for循环中将多个图层添加到layergroup。 map.getLayerGroup(yearcmpGrp).addLayer(samplevectorlyr)无法正常工作
解决方法
这有效
yearcmpGrp.getLayers().push(samplevectorlyr);
感谢@Mike。我发布的答案是bcz,可能对某人有帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。