VectorLayer 不切换可见性,始终打开
VectorLayer don't toggle visibity, always turned on
我有一个带有用户生成的一些点的 VectorLayer,配置如下:
var sourcePoint = new VectorSource();
var vectorPoint = new VectorLayer({
source: sourcePoint,
style: new Style({
fill: new Fill({
color: 'rgba(0, 0, 0, 0.2)'
}),
image: new CircleStyle({
radius: 7,
fill: new Fill({
color: '#32ff32'
})
})
}),
zIndex: 99,
name: "vectorPoint",
visible: false
});
当地图页面打开时,它会加载用户数据并填充到 sourcePoint 中。问题是这个 vectorLayer 必须以可见性设置为 false 开始,但即使 visible: false
它也开始打开。
我还尝试使用 setVisible()
功能切换此图层的可见性,但该功能也不适用于此图层。奇怪的是,其他 VectorLayer 工作正常,从可见性设置为关闭和切换开始。
发生这种情况有什么原因吗?我找不到有类似问题的人。
更新 17/02/2020
我如何将图层添加到地图:
this.map = new Map({
layers: [tileBackground, vectorPoint],
target: document.getElementById('map'),
view: view
});
我正在使用的基本切换图层功能
randomFunction(status, layerName) { // Firing as randomFunction(true, 'vectorPoint')
this.map.getLayers().forEach((layer) => {
if (layer.get('name') == layerName && layer.get('name') != undefined) {
if (status == true) {
layer.setVisible(true);
} else {
layer.setVisible(false);
}
}
});
}
可以看到层是在if (layer.get('name') == layerName...
之后建立的
好的,我设法找到了问题所在。在这种情况下的错误是我有其他矢量图层用于使用设备的 GPS 创建的点。加载点时,它们填充了两个层,因此当我尝试关闭主要点层时,另一个仍然处于活动状态,误导我相信错误出在我使用的方法上。很抱歉给您带来麻烦。
我有一个带有用户生成的一些点的 VectorLayer,配置如下:
var sourcePoint = new VectorSource();
var vectorPoint = new VectorLayer({
source: sourcePoint,
style: new Style({
fill: new Fill({
color: 'rgba(0, 0, 0, 0.2)'
}),
image: new CircleStyle({
radius: 7,
fill: new Fill({
color: '#32ff32'
})
})
}),
zIndex: 99,
name: "vectorPoint",
visible: false
});
当地图页面打开时,它会加载用户数据并填充到 sourcePoint 中。问题是这个 vectorLayer 必须以可见性设置为 false 开始,但即使 visible: false
它也开始打开。
我还尝试使用 setVisible()
功能切换此图层的可见性,但该功能也不适用于此图层。奇怪的是,其他 VectorLayer 工作正常,从可见性设置为关闭和切换开始。
发生这种情况有什么原因吗?我找不到有类似问题的人。
更新 17/02/2020
我如何将图层添加到地图:
this.map = new Map({
layers: [tileBackground, vectorPoint],
target: document.getElementById('map'),
view: view
});
我正在使用的基本切换图层功能
randomFunction(status, layerName) { // Firing as randomFunction(true, 'vectorPoint')
this.map.getLayers().forEach((layer) => {
if (layer.get('name') == layerName && layer.get('name') != undefined) {
if (status == true) {
layer.setVisible(true);
} else {
layer.setVisible(false);
}
}
});
}
可以看到层是在if (layer.get('name') == layerName...
好的,我设法找到了问题所在。在这种情况下的错误是我有其他矢量图层用于使用设备的 GPS 创建的点。加载点时,它们填充了两个层,因此当我尝试关闭主要点层时,另一个仍然处于活动状态,误导我相信错误出在我使用的方法上。很抱歉给您带来麻烦。