getFeatures()为空 [英] getFeatures() is empty

查看:772
本文介绍了getFeatures()为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的问题,例如 OpenLayer 3中的POI ,但并非相同.

I have a similar question like POI in OpenLayer 3 but not the same.

我正在使用KML文件绘制地图:

I'm drawing a map with an KML File:

var layerVector = new ol.layer.Vector({
    source : new ol.source.KML({
        projection : projection,
        url : _url
    }),
});

它工作正常,我可以在地图上看到KML文件中的点.

It works fine and I can see the points from the KML file in the map.

(Using <Style> and <styleUrl> Tags in the KML file.)

但是,如果我尝试查看功能,则会得到一个空数组.

But if I try to see the features i get an empty Array.

console.log( layerVector.getSource().getFeatures() );

==> Array [ ]

有什么主意吗?

thx mx

推荐答案

在我们即将发布的常见问题解答中查看答案:

See the answer in our upcoming FAQ:

https://github.com/openlayers/ol3/blob/master/doc/faq.md

来自上面的链接:

为什么我的源代码中没有任何功能?

Why aren't there any features in my source?

假设您要加载KML文件并在地图上显示包含的要素.可以使用如下代码:

Suppose you want to load a KML file and display the contained features on the map. Code like the following could be used:

var vector = new ol.layer.Vector({
  source: new ol.source.KML({
    projection: 'EPSG:3857',
    url: 'data/kml/2012-02-10.kml'
  })
});

您可能会问自己该KML中有多少个功能,然后尝试执行以下操作:

You may ask yourself how many features are in that KML, and try something like the following:

var vector = new ol.layer.Vector({
  source: new ol.source.KML({
    projection: 'EPSG:3857',
    url: 'data/kml/2012-02-10.kml'
  })
});
var numFeatures = vector.getSource().getFeatures().length;
console.log("Count right after construction: " + numFeatures);

这将记录源中包含0个功能的计数.这是因为KML文件的加载将以异步方式进行.为了尽快获得计数(在获取文件并在源中填充了功能之后),应该在源上使用事件侦听器功能:

This will log a count of 0 features to be in the source. This is because the loading of the KML-file will happen in an asynchronous manner. To get the count as soon as possible (right after the file has been fetched and the source has been populated with features), you should use an event listener function on the source:

vector.getSource().on('change', function(evt){
  var source = evt.target;
  if (source.getState() === 'ready') {
    var numFeatures = source.getFeatures().length; 
    console.log("Count after change: " + numFeatures);
  }
});

这将正确报告功能数量,在特定情况下为1119.

This will correctly report the number of features, 1119 in that particular case.

这篇关于getFeatures()为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆