0
votes

i am trying to autorefresh a WMS layer from openlayers 3.5 every x minutes.

i have defined a layer like this:

var radar = new ol.layer.Image({
    name: 'radar',
    visible: true,
    source: new ol.source.ImageWMS({
        url: 'geoserver url',
        params: {
            'LAYERS': 'xxxx',
            'SRS': 'EPSG:4326',
            'FORMAT': 'image/png',
            'SERVICE': 'WMS',
            'VERSION': '1.1.0',
            'REQUEST': 'GetMap',               
            'WIDTH': '512',
            'HEIGHT': '376'
        }
    })
});

when i initialize the map, the layer shows correctly. Then i use "later js library" to refresh the layer every x minutes and here i have my doubt. i have a function to refresh the layer:

    function refreshRadarLayer() {
map.removeLayer(radar);

radar = new ol.layer.Image({
    name: 'radar',
    source: new ol.source.ImageWMS({
        url: 'geoserver url',
        params: {
            'LAYERS': 'xxxxx',
            'SRS': 'EPSG:4326',
            'FORMAT': 'image/png',
            'SERVICE': 'WMS',
            'VERSION': '1.1.0',
            'REQUEST': 'GetMap',           
            'WIDTH': '512',
            'HEIGHT': '376'
        }
    })
});

map.addLayer(radar);

}

this function goes right but, can i use setSource to do the same like this?

function refreshRadarLayer() {
    radar.setSource(new ol.source.ImageWMS({
        url: 'geoserver url',
        params: {
            'LAYERS': 'xxxx',
            'SRS': 'EPSG:4326',
            'FORMAT': 'image/png',
            'SERVICE': 'WMS',
            'VERSION': '1.1.0',
            'REQUEST': 'GetMap',      
            'WIDTH': '512',
            'HEIGHT': '376'
        }
    }));
}
2

2 Answers

0
votes

try to update the source parameters, not the source itself. like so

radar.getSource().updateParams({
'LAYERS': 'xxxx',
'SRS': 'EPSG:4326',
'FORMAT': 'image/png',
'SERVICE': 'WMS',
'VERSION': '1.1.0',
'REQUEST': 'GetMap',      
'WIDTH': '512',
'HEIGHT': '376',
't':new Date().getMilliseconds()
});
0
votes

Thanks,

finally i resolved the autorefresh similar as your answer:

radar.getSource().updateParams({
            'LAYERS': 'xxxx',
            'TIME': n
    });

where n is a function that returns milliseconds.