TLDR я хочу обновить слой на таймере, таким образом, он отображает новые kml данные на графике (как ссылка обновления / сетевой канал)
До сих пор я попробовал функцию действия следующим образом:
function RefreshKMLData(layer) {
layer.loaded = false;
layer.setVisibility(true);
layer.redraw({ force: true });
}
интервал набора функции:
window.setInterval(RefreshKMLData, 5000, KMLLAYER);
сам слой:
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
})
})
});
URL для KMLLAYER с Математикой, случайной, таким образом, это не кэшируется:
var MYKMLURL = var currentanchorpositionurl = 'http://' + host + '/data?_salt=' + Math.random();
Я думал бы, что это Обновит слой. Как путем установки его загруженного на ложь разгружает его. Видимость к истинным перезагрузкам это и со случайной Математикой не должно позволять этому кэшироваться? Таким образом, кто-либо сделал это прежде или знает, как я могу заставить это работать?
Как видно, мне было достаточно сложно найти информацию по этому поводу, я бы добавил следующее:
1)
Создайте слой KML:
//Defiine your KML layer//
var MyKmlLayer= new OpenLayers.Layer.Vector("This Is My KML Layer", {
//Set your projection and strategies//
projection: new OpenLayers.Projection("EPSG:4326"),
strategies: [new OpenLayers.Strategy.Fixed()],
//set the protocol with a url//
protocol: new OpenLayers.Protocol.HTTP({
//set the url to your variable//
url: mykmlurl,
//format this layer as KML//
format: new OpenLayers.Format.KML({
//maxDepth is how deep it will follow network links//
maxDepth: 1,
//extract styles from the KML Layer//
extractStyles: true,
//extract attributes from the KML Layer//
extractAttributes: true
})
})
});
2)
Установите URL-адрес для слоя KML :
//note that I have host equal to location// //Math.Random will stop caching//
var mykmlurl = 'http://' + host + '/KML?key=' + Math.random();
3)
Установите интервал обновления слоя:
//function called// //timer// //layer to refresh//
window.setInterval(UpdateKmlLayer, 5000, MyKmlLayer);
4)
Функция обновления слоя:
function UpdateKmlLayer(layer) {
//setting loaded to false unloads the layer//
layer.loaded = false;
//setting visibility to true forces a reload of the layer//
layer.setVisibility(true);
//the refresh will force it to get the new KML data//
layer.refresh({ force: true, params: { 'key': Math.random()} });
}
Надеюсь, это упростит задачу для некоторых других.