javascript OL3:强制重绘图层
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22744112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OL3: force redraw layer
提问by user2473933
i am currently upgrading OpenLayers client version 2.13.1 with the new version of OpenLayers, OL3. My setup consist of Mapserver as a WMS mapping server and previously mentioned OpenLayers client.
我目前正在使用新版本的 OpenLayers OL3 升级 OpenLayers 客户端版本 2.13.1。我的设置包括 Mapserver 作为 WMS 映射服务器和前面提到的 OpenLayers 客户端。
In old system I made support for user interaction in the way that if a user click on some part of map, the mapfile gets generated again, and as a result I force to redraw layer to get some part of map colored. Example of the code in the OL2 version:
在旧系统中,我支持用户交互的方式是,如果用户单击地图的某个部分,地图文件会再次生成,因此我强制重绘图层以使地图的某些部分着色。OL2版本中的代码示例:
$.ajax({
params: ...
success: function (data) {
if (data.success) {
gisLayer.redraw(true);
}
}
});
I want to get the same functionality in OL3, but there is no redraw function. I found two functions that are useful, but there is additional things to do in order to get the same functionality: - layer.getSource().updateParams(params); and - map.render();
我想在 OL3 中获得相同的功能,但没有重绘功能。我发现了两个很有用的函数,但为了获得相同的功能,还有其他事情要做: - layer.getSource().updateParams(params); 和 - map.render();
I also created a little bit more complicated example, in which I get code to working, but the requests for getting WMS tiles contains additional parameter as a key to get requests unique. The example code is above:
我还创建了一个稍微复杂一点的示例,在该示例中我让代码工作,但是获取 WMS 切片的请求包含附加参数作为获取唯一请求的键。示例代码如上:
var params = layer.getSource().getParams();
params.t = getUniqueParam();
layer.getSource().updateParams(params);
Ok, That is the situation, all I want to ask is if there is any function available, that can force layers to be redrawn, without adding aditional parameter in the WMS requests? AFAIK the "problem" is that browser cache images, and if the request is the same, that was before, browser shows old images again.
好的,就是这种情况,我想问的是是否有任何可用的功能,可以强制重绘图层,而无需在WMS请求中添加附加参数?AFAIK“问题”是浏览器缓存图像,如果请求是相同的,那就是之前,浏览器再次显示旧图像。
Thanks for any help.
谢谢你的帮助。
回答by Danny Hoek
Can you check if this does the trick?
你能检查一下这是否有效吗?
yourLayerSource.dispatchChangeEvent();
回答by N Dorigatti
none of the above worked for me, i also tried everything together:
以上都不适合我,我也一起尝试了所有方法:
var params = yourLayer.getSource().getParams();
yourLayer.getSource().updateParams(params);
yourLayer.getSource().dispatchChangeEvent();
map.render();
Nothing happens,no redraw, no network requests (either cached or not)... The documentation is really bad about that. I've tried to check in the source code for the event thrown when panning but is kinda impossible to understand...
什么都没有发生,没有重绘,没有网络请求(无论是否缓存)......文档对此非常糟糕。我试图检查平移时抛出的事件的源代码,但有点无法理解......
EDIT: I manage to make it work!
编辑:我设法让它工作!
$(document).ready(function(){
map.once("postcompose", function(){
//start refreshing each 3 seconds
window.setInterval(function(){
// call your function here
var params = yourLayer.getSource().getParams();
params.t = new Date().getMilliseconds();
yourLayer.getSource().updateParams(params);
}, 3000);
});
});