我已经将一些 GeoJson 加载到 openlayers 3 矢量图层中
var countriesLayer = new ol.layer.Vector({
source: new ol.source.Vector({
url: '/data/countriesandstates.geojson',
format: new ol.format.GeoJSON()
}),
style: function(feature, resolution) {
countriesLayerTextStyle.getText().setText(resolution < 5000 ? feature.get('name') : '');
return [countriesLayerStyle, countriesLayerTextStyle];
}
});
我想使用
运行该源中的所有功能countriesLayer.getSource().forEachFeature(...);
但是它从不调用我的回调,如果我尝试 getFeatures()
我会得到一个空数组。但是它呈现得很好,所以我知道数据已加载。我什至尝试在 5 秒后超时执行以确保它已加载和解析。
我做错了什么?
最佳答案
这似乎可行:
countriesLayer.getSource().on("change", function(ev) {
if( countriesLayer.getSource().getState() === "ready" ) {
console.log(countriesLayer.getSource().getFeatures().length)
}
});
关于javascript - 如何从 geoJson 加载中获取特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38146603/