我正在研究一个使用Openlayers和geoserver的项目,我的目标是放大我分配给用户的国家/地区。目前,我是通过添加ol.format.filter.equalTo('country','India')这样的过滤器来实现的,但是问题是,如果我分配了1个以上的国家,则必须更改代码,以便多个ol.format.filter.equalTo,我想动态添加它们。
我搜索了以前的答案,但没有一个能解决这个问题。
这是我的示例代码。请看一下。
var conditions=new Array();
for(var i=0;i<this.selectedAttribute.length;i++) {
if(this.selectedAttribute[i]=="") {
alert("Please selected all attributes");
return;
}
if(this.selectedValue[i]=="") {
alert("Please input value");
return;
}
if(this.selectedOperator[i]=="") {
alert("Please select operators");
return;
}
var condition;
var operator=this.selectedOperator[i];
if(operator=="=") {
condition=ol.format.filter.equalTo(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator=="!=") {
condition=ol.format.filter.equalTo(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator=="<"){
condition=ol.format.filter.greaterThan(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator=="<="){
condition=ol.format.filter.greaterThanOrEqualTo(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator==">"){
condition=ol.format.filter.lessThan(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator==">="){
condition=ol.format.filter.lessThanOrEqualTo(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator=="LIKE"){
condition=ol.format.filter.like(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator=="ISNULL"){
condition=ol.format.filter.isNull(this.selectedAttribute[i]);
}
conditions.push(condition);
}
if(this.selectedAttribute.length==1) {
filter=conditions[0];
}
else {
if(this.selectedCondition=="AND") {
filter=ol.format.filter.and.apply(null,conditions);
}
else if(this.selectedCondition=="OR"){
filter=ol.format.filter.or.apply(null,conditions);
}
}