i'm trying to show only a certain feature from my Vector Source. so i make a function like this:
function showUtility(id) {
utilitiesSource.forEach(function(i) {
//console.log(i.getFeatures());
i.forEachFeature(function(f) {
if (f.getProperties().id == id) {
f.setStyle(null);
} else {
f.setStyle([]);
}
});
});
}
where the utilitiesSource comes from:
utilitiesSource = [];
var utilitySource = new ol.source.Vector({
format: new ol.format.GeoJSON(),
projection: 'EPSG:4326',
url: 'someUrl',
extractStyles: false
});
utilitiesSource.push(utilitySource);
the problem is console.log(i.getFeatures())
in showUtility()
always gives empty array although it's fine if i was using it in an eventlistener like these:
var selectCtrl = new ol.control.Select({
source: utilitiesSource,
property: $(".options select").val(),
selectLabel: 'Cari',
addLabel: 'Tambah Kondisi',
allLabel: 'Cocokkan Semua',
attrPlaceHolder: 'atribut',
valuePlaceHolder: 'nilai'
});
selectCtrl.on('select', function(e) {
select.getFeatures().clear();
utilitiesSource.forEach(function(i) {
//console.log(i.getFeatures()); --> gives array of feature
i.forEachFeature(function(f) {
f.setStyle([]);
});
})
e.features.forEach(function(f) {
f.setStyle(null);
});
});
var bar = new ol.control.Bar({
group: true,
controls: [
selectCtrl,
new ol.control.Button({
html: '<i class="fa fa-undo" ></i>',
title: 'Reset',
handleClick: function() {
select.getFeatures().clear();
utilitiesSource.forEach(function(i) {
//console.log(i.getFeatures()); --> gives array of feature
i.getFeatures().forEach(function(f) {
f.setStyle(null);
});
})
}
})
]});
and if i use browser's console and execute utilitiesSource.forEach(function(i) { i.getFeatures().forEach(function(j){ if (j.getProperties().id == 100) {j.setStyle(null);} else {j.setStyle([])} });});
i get what i want
why is that? why can't i get the features in showUtility(id)?
(still not enough reputation so i have to ask questions awkwardly in the comments -.-)
have you tried calling your function after a setTimeout()? The geojson source loads the data asynchronously, so even calling it at the last line of code might be too early. I think this should still be up-to-date.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.