views:

632

answers:

3

Here is an example on: How to Refresh / Reload a KML layer in OpenLayers. Dynamic KML Layer. See my answer below.


TLDR See my answer below on how to refresh the layer.

A: 

Figured seen as it was hard enough for me to find information on this I would add this:


1)

Create the KML Layer:

            //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)

Set the URL for the KML Layer:

//note that I have host equal to location//   //Math.Random will stop caching//
var mykmlurl = 'http://' + host + '/KML?key=' + Math.random();

3)

Set the interval in which to refresh your layer:

           //function called// //timer// //layer to refresh//
window.setInterval(UpdateKmlLayer, 5000, MyKmlLayer);

4)

The function to update the layer:

            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()} });
                //- <3 from Thqr -//
            }

Hopes this makes it easier for some others out there.

Thqr
Would be nice to know why this got -1. I asked a question and found the solution myself about 4days later and updated it? Please explain why helping the SO community is worth a downvote?
Thqr
Why don't you use OpenLayers.Strategy.Refresh (http://dev.openlayers.org/apidocs/files/OpenLayers/Strategy/Refresh-js.html) instead of the fixed strategy? It was exactly designed for your purpose.
Franz
I am using refresh.. The rest is to stop caching issues accross broswers IE/FF/Chrome/Safari/Opera... and the reason for having it in a function is that I can call it multiple times for multiple layers. Using visiblity to true FORCES it to reload even if it decides to cache it normally in IE7-
Thqr
A: 

where did you use proposedanchorpositionurl ?

mr w
Fixed see changes
Thqr
A: 

this code is not working... pois is kmllayer object, but it s not working :( and there is no error

function busAnim() {

    for(counter=0; counter<<?=$ii?>+1; counter++) {

        pois.loaded = false;
        pois.setVisibility(true);
        pois.refresh({ force: true, params: { 'key': Math.random() } });
        document.getElementById("debugger1").value=parseInt(document.getElementById("debugger1").value)+1;
        alert("<?php print $x_startdatetime."<<"; ?> " + counter);
        usleep(2000000);
        //alert(counter);
    }
}
function usleep(microseconds) {
    var start = new Date().getTime();
    while (new Date() < (start + microseconds/1000));
    return true;
}




<a href="javascript:busAnim();">Run</a>
mr w
is your function after you have created and defined the map? You would be better asking this as a question not submitting it as an answer.
Thqr