我和这件事斗争了很久。挑战是,我可以在一种样式上画几个不同的层,然后调用setStyle (例如:去卫星视图),它清除了我所有的层。
发布于 2018-08-27 00:14:20
我通过复制我关心的层和源并重新应用它们来解决这个问题。
function forEachLayer(text, cb) {
this.map.getStyle().layers.forEach((layer) => {
if (!layer.id.includes(text)) return;
cb(layer);
});
}
// Changing the map base style
export function changeStyle(style) {
const savedLayers = [];
const savedSources = {};
const layerGroups = [
'layer-type-1',
'layer-type-2'
];
layerGroups.forEach((layerGroup) => {
this.forEachLayer(layerGroup, (layer) => {
savedSources[layer.source] = this.map.getSource(layer.source).serialize();
savedLayers.push(layer);
});
});
this.map.setStyle(`mapbox://styles/mapbox/${style}`);
setTimeout(() => {
Object.entries(savedSources).forEach(([id, source]) => {
this.map.addSource(id, source);
});
savedLayers.forEach((layer) => {
this.map.addLayer(layer);
});
}, 1000);
}https://stackoverflow.com/questions/52031176
复制相似问题