Javascript 如何在构建OlMap后向其添加新层
我在Javascript 如何在构建OlMap后向其添加新层,javascript,reactjs,openlayers,openlayers-3,Javascript,Reactjs,Openlayers,Openlayers 3,我在构造函数我的类中构建了一个映射,如下所示: constructor(props) { super(props); this.mapDivId = `map-${Math.random()}`; const baseLayer = new OlLayerTile({ name: 'OSM', source: new OlSourceOSM() }) this.map = new OlMap({ //A
构造函数
我的类中构建了一个映射,如下所示:
constructor(props) {
super(props);
this.mapDivId = `map-${Math.random()}`;
const baseLayer = new OlLayerTile({
name: 'OSM',
source: new OlSourceOSM()
})
this.map = new OlMap({
//Assign Layers
layers: [
baseLayer,
],
view: new OlView({
center: fromLonLat(VIEW_CENTER),
zoom: ZOOM,
})
});
}
<Map layers={layers}/>
现在一切都很好,当我在构造函数中时,我可以添加任何我想要的层,现在问题是,我想通过我的类的道具(当调用某个api时)传递新层,如下所示:
constructor(props) {
super(props);
this.mapDivId = `map-${Math.random()}`;
const baseLayer = new OlLayerTile({
name: 'OSM',
source: new OlSourceOSM()
})
this.map = new OlMap({
//Assign Layers
layers: [
baseLayer,
],
view: new OlView({
center: fromLonLat(VIEW_CENTER),
zoom: ZOOM,
})
});
}
<Map layers={layers}/>
我如何处理向我的地图添加新图层的问题?我只想在图层道具更改时更改地图的图层调用addLayer后是否尝试渲染地图?@A.Roussos,我是openlayer的完全初学者,如何重新渲染地图?,目前我尝试了另一种解决方案,我给我的组件提供了一个新的
键,并强制dom呈现一个全新的组件。这意味着构造函数将被强制再次运行,我得到了我想要的结果,但我不确定这是否是最好的选择。试试map.render()map.addLayer(layer)
应该可以正常工作。。你能添加更多关于图层的信息吗?你是如何设置道具的。。顺便说一句,你应该在添加之前删除图层,如果不是,你是在反映你正在累积图层的道具的变化