ol6中如何动态添加多个矢量图层到图层组?
How to dynamically add multiple vector layers to a layer group in 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,0], width: 2
})
})
}),
opacity: 0.5,
});
//map.addLayer(samplevectorlyr); //this works fine & add a new layer outside layer group
map.getLayerGroup(yearcmpGrp).addLayer(samplevectorlyr); //This don't work !!
}
我想在 for 循环中向图层组添加多个图层。 map.getLayerGroup(yearcmpGrp).addLayer(samplevectorlyr) 不工作
这有效
yearcmpGrp.getLayers().push(samplevectorlyr);
谢谢@迈克。我作为答案发布,因为它可能会对某人有所帮助。
我想通过单击按钮将图层动态添加到图层组。 我添加了一个 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,0], width: 2
})
})
}),
opacity: 0.5,
});
//map.addLayer(samplevectorlyr); //this works fine & add a new layer outside layer group
map.getLayerGroup(yearcmpGrp).addLayer(samplevectorlyr); //This don't work !!
}
我想在 for 循环中向图层组添加多个图层。 map.getLayerGroup(yearcmpGrp).addLayer(samplevectorlyr) 不工作
这有效
yearcmpGrp.getLayers().push(samplevectorlyr);
谢谢@迈克。我作为答案发布,因为它可能会对某人有所帮助。