按类(Vector)获取图层,然后使用Openlayer 2.14刷新不起作用

时间:2016-02-05 07:40:57

标签: javascript openlayers

首先,我得到所有Vector层然后评估它是否可见并得到它的名字。然后我想删除该图层上的所有过滤器,然后刷新。

以下是代码片段:

var mLayers = map.getLayersByClass("OpenLayers.Layer.Vector");
for(var a = 0; a < mLayers.length; a++ ){
    if(mLayers[a].getVisibility()){     
        var layerName = mLayers[a].name;
        var vlayer = map.getLayersByName(layerName);
        //console.log(vlayer);
        vlayer.filter = null;
        vlayer.refresh({
            force: true
        });         
    }
};

它会抛出错误:

  

未捕获TypeError:vlayer.refresh不是函数

我注意到如果我使用了矢量图层的指定变量,则刷新工作。

例如:

var vector_bldg =new OpenLayers.Layer.Vector("Buildings", {
...
}

然后

vector_bldg .filter = null;
vector_bldg .refresh({
    force: true
});

1 个答案:

答案 0 :(得分:1)

检查控制台时,图层没有刷新功能的原因是它无法访问变量。 要解决这个问题,请不要使用:

var mLayers = map.getLayersByClass("OpenLayers.Layer.Vector");
for(var a = 0; a < mLayers.length; a++ ){
    if(mLayers[a].getVisibility()){     
        var layerName = mLayers[a].name;
        var vlayer = map.getLayersByName(layerName);
        //console.log(vlayer);
        vlayer.filter = null;
        vlayer.refresh({
            force: true
        });         
    }
};

只需将变量更改为: enter image description here

var mLayers = map.getLayersByClass("OpenLayers.Layer.Vector");
    for(var a = 0; a < mLayers.length; a++ ){
        if(mLayers[a].getVisibility()){     
            var layerName = mLayers[a].name;
            var vlayer = map.getLayersByName(layerName);
            //console.log(vlayer);
            vlayer[0].filter = null;
            vlayer[0].refresh({
                force: true
            });         
        }
    };