How to Refresh / Reload a KML layer in OpenLayers. Dynamic KML Layer.
- by Ozaki
TLDR See my answer below on how to refresh the layer.
So far I have tried action function as follows:
function RefreshKMLData(layer) {
layer.loaded = false;
layer.setVisibility(true);
layer.redraw({ force: true });
}
set interval of the function:
window.setInterval(RefreshKMLData, 5000, KMLLAYER);
the layer itself:
var KMLLAYER = new OpenLayers.Layer.Vector("MYKMLLAYER", {
projection: new OpenLayers.Projection("EPSG:4326"),
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: MYKMLURL,
format: new OpenLayers.Format.KML({
extractStyles: true,
extractAttributes: true
})
})
});
the url for KMLLAYER with Math random so it doesnt cache:
var MYKMLURL = var currentanchorpositionurl = 'http://' + host + '/data?_salt=' + Math.random();
I would have thought that this would Refresh the layer. As by setting its loaded to false unloads it. Visibility to true reloads it and with the Math random shouldn't allow it to cache? So has anyone done this before or know how I can get this to work?
TLDR See my answer below on how to refresh the layer.