首页 > 其他 > 详细

ol.source.Vector.getFeatures() 填坑

时间:2021-07-01 09:28:41      阅读:23      评论:0      收藏:0      [点我收藏+]

创建矢量图层

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

休息一下。。。

 

ol.source.Vector.getFeatures() 填坑

原文:https://www.cnblogs.com/shenyc/p/14956951.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!