This is a static archive of our old OpenStreetMap Help Site. Please post any new questions and answers at community.osm.org.

[closed] How to show KML in OSM with automatically refresh ?

0

I've got a complexion with this JavaScript. I've done several research but yet to succeed. Anyone?

<script type="text/javascript">
var map;

function init() {
// Create the map object
map = new OpenLayers.Map({
        allOverlays: true,
        controls: [
            new OpenLayers.Control.Navigation(),
            new OpenLayers.Control.PanZoom(),
        ]
    });
    var osm_layer = new OpenLayers.Layer.OSM();
    map.addLayer(osm_layer);

map.setCenter(
            new OpenLayers.LonLat(mylon,mylat).transform(
                new OpenLayers.Projection("EPSG:4326"),
                map.getProjectionObject()
            ), 12
        ); */        
}

var KMLURL = 'myfile.kml?' + Math.random();

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: KMLURL,
            //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
            })
        })
    });

window.setInterval(UpdateKmlLayer, 5000, MyKmlLayer);

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()} });
    }
}
</script>

Thank you in advance.

asked 05 Aug '11, 08:46

zearth's gravatar image

zearth
21335
accept rate: 0%

closed 05 Aug '11, 09:41

Jonathan%20Bennett's gravatar image

Jonathan Ben...
8.3k1785108

2

First, this isn't really an OpenStreetMap question, but an OpenLayers one (or even just a JavaScript one) -- you may get an answer by asking in a forum for OpenLayers.

Even then you should try to come up with a better worded question, rather than simply dumping your code and expecting everyone to find the problem. State what you've tried that didn't work, and what results you got instead.

(05 Aug '11, 09:45) Jonathan Ben...

The question has been closed for the following reason “Question is off-topic or not relevant” by Jonathan Bennett 05 Aug ‘11, 09:41

Source code available on GitHub .