OL3: force redraw layer

11,158

Solution 1

Can you check if this does the trick?

yourLayerSource.dispatchChangeEvent(); 

Solution 2

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);
    });
});
Share:
11,158

Related videos on Youtube

user2473933
Author by

user2473933

Updated on June 04, 2022

Comments

  • user2473933
    user2473933 about 2 years

    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.

    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:

    $.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();

    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:

    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.

    Thanks for any help.

  • ProfNimrod
    ProfNimrod over 9 years
    getParams() is not supported in version 3.0.0.
  • ProfNimrod
    ProfNimrod over 9 years
    It seems getParams is only supported for ol.source.ImageMapGuide, ol.source.ImageWMS, ol.source.TileWMS, or at least these are the sources found in the current documentation.
  • N Dorigatti
    N Dorigatti over 9 years
    Oh, yeah sorry. I was assuming the usage of ImageWMS or TileWMS because those are the layers i use (since the starting discussion was about WMS layers). Which layer are you using instead?
  • Alnitak
    Alnitak about 9 years
    in OL3.5, I've used source.dispatchEvent('change')
  • Abhijit Gujar
    Abhijit Gujar over 8 years
    do we have to do anything while publishing WMS service for this ? like any extra attribute ?
  • N Dorigatti
    N Dorigatti over 8 years
    no, absolutely. The code above should be enough (at least with the versione of OL3 available when at that time) to refresh the layer