如何从 geoJson 加载中获取特征?
How do I get features from a geoJson load?
我已经将一些 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 秒后超时执行以确保它已加载和解析。
我做错了什么?
当您的功能添加(AJAX)到源时收听:
countriesLayer.getSource().on('addfeature', function() {
// process further
});
// or just once
countriesLayer.getSource().once('addfeature', function() {
// process further
});
这似乎有效:
countriesLayer.getSource().on("change", function(ev) {
if( countriesLayer.getSource().getState() === "ready" ) {
console.log(countriesLayer.getSource().getFeatures().length)
}
});
我已经将一些 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 秒后超时执行以确保它已加载和解析。
我做错了什么?
当您的功能添加(AJAX)到源时收听:
countriesLayer.getSource().on('addfeature', function() {
// process further
});
// or just once
countriesLayer.getSource().once('addfeature', function() {
// process further
});
这似乎有效:
countriesLayer.getSource().on("change", function(ev) {
if( countriesLayer.getSource().getState() === "ready" ) {
console.log(countriesLayer.getSource().getFeatures().length)
}
});