【发布时间】:2017-03-08 10:30:31
【问题描述】:
我有以下函数,它从选择中获取一个选项,并通过我的 geojson 文件中的参数进行匹配。如果找到完全匹配,则该图层应以特定样式显示。
一切似乎都很好,因为该函数会为每个特征找到合适的对象,它实际上会获取名为“Criteria”的字符串并将其放入一个包含所有条件的数组中(例如,array = {"A", "B" , "Bi", "Bii"}。它还会检查所选选项是否与数组中的元素之一匹配。但是它不会输出命令setStyle。
有什么建议吗?完整的 plunker 可以在here 找到。
//when clicking the select
document.getElementById("criteria_select").addEventListener('click', function (){
//first step it loads the vector layer and sets the style to transparent
var i;
for (i = 0; i < layers_group.length; ++i){
layers_group[i].setStyle(transparentStyle);
layers_group[i].setVisible(true);
};
//second step when the select changes (i.e. I make a choice)
$("#criteria_select").change(function() {
//it starts the function to change styles according to the selection made
var selectedCriteria = $("#criteria_select").val();
console.log('selectedCriteria',selectedCriteria);
var index;
for (index = 0; index < source_group.length; index++){
source_group[index].forEachFeature(function(feat){
var array = feat.get('Criteria').split(',');
for (i = 0; i < array.length; i++)
{
console.log(array[i]);
if (selectedCriteria === array[i]){
feat.setStyle(selectStyle);
} else {
//and if doesnt exist switch back to the deafult style
feat.setStyle(transparentStyle);
}
}
});
};
});
});
【问题讨论】:
标签: for-loop openlayers-3