Vectorlayer.getSource().getFeatures() return empty array
最近在一个vue项目中使用openlayer来做地图开发,导入的矢量数据都是以geojson的格式来存储和读取的。在地图中加载完成后,能成功的对地图上的要素进行点击弹窗,显示要素的对应属性。但是想根据图层来直接获取要素时结果却为空值,示例代码如下:
let Vectorlayer=new VectorLayer({
title: '边界',
name: 'baseVectorLayer',
baseLayer: false,
source: new VectorSource({
url:'data/voto.geojson',
format: new GeoJSON()
}),
zIndex: 1,
style: new Style({
stroke: new Stroke({
color: '#ffcc33',
width: 2
}),
})
})
console.log(Vectorlayer.getSource().getFeatures())//显示为[]
可以明确的是,以geojson格式导入的ol/layer/Vector图层是会有要素记录的,毕竟我可以点击显示。返回值为空的原因在StackOverflow的这篇问答中有了答案:https://stackoverflow.com/questions/29432645/getfeatures-is-empty。答主也只是给了一篇回答