ol.source.Vector.getFeatures() 填坑

创建矢量图层

1 var vectorChina = new ol.layer.Vector({
2     source: new ol.source.Vector({
3         projection : 'EPSG:4326',
4         url: 'geojson/china.json',
5         format: new ol.format.GeoJSON()
6     }),
7     zIndex: 1,
8     visible:true
9 });

从数据源取得要素集合时出现问题

var features = vectorChina.getSource().getFeatures();
console.log(features);  // 调试显示 undefined

各种折腾。。。各种折腾。。。

终于。。。

静态资源文件的加载将是以异步方式进行的,需要在监听事件中进行处理

静态资源文件的加载将是以异步方式进行的,需要在监听事件中进行处理

静态资源文件的加载将是以异步方式进行的,需要在监听事件中进行处理

然后

 1 vectorChina.on('change', function (evt) {
 2     // 获取矢量图层数据源
 3     var source = evt.target.getSource();
 4     if (source.getState() === 'ready') {
 5         // 获取数据集
 6         var features = source.getFeatures();
 7         console.log(features);
 8         console.log(features.length);
 9         // console.log(features[0].get('要素名称'));
10     }
11 });

OK

休息一下。。。

 

posted on 2021-06-30 23:34  shenyczz  阅读(587)  评论(0编辑  收藏  举报

导航