OL3: force redraw layer

I am currently updating the version of OpenLayers version 2.13.1 with the new version of OpenLayers, OL3. My setup consists of Mapserver as the WMS mapping server and the previously mentioned OpenLayers client.

In the old system, I supported user interaction in such a way that if the user clicks on some part of the map, the map file is generated again, and as a result I force the layer to redraw to get some part of the map. Example code in 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 useful functions, but there are additional things to do the same functionality: - layer.getSource (). updateParams (params); as well as map.render ();

I also created a slightly more complex example in which I get the code to work, but requests for receiving WMS-plates contain an additional parameter as a key for receiving unique requests. Sample code above:

 var params = layer.getSource().getParams(); params.t = getUniqueParam(); layer.getSource().updateParams(params); 

Good. This is the situation I want to ask, is there any function that can make layers redraw without adding an additional parameter to WMS requests? AFAIK "problem" is that the browser cache images, and if the request is the same as before, the browser displays old images again.

Thanks for any help.

+7
javascript openlayers openlayers-3
source share
2 answers

Can you check if this does the trick?

 yourLayerSource.dispatchChangeEvent(); 
+5
source share

None of the above worked for me, I also tried all together:

 var params = yourLayer.getSource().getParams(); yourLayer.getSource().updateParams(params); yourLayer.getSource().dispatchChangeEvent(); map.render(); 

Nothing happens, no redrawing, no network requests (either cached or not) ... The documentation is really bad about that. I tried to check the source code of the event that was selected when panning, but it is impossible to understand ...

EDIT: I manage to get it working!

 $(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); }); }); 
+2
source share

All Articles