Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Arguably, this is a easy question for you guys GIS expert. Here is the question, how to outsource a dynamic KML file using JavaScript in OpenLayers. I want to refresh every 7 seconds.

Below are snippet of the given codes:

<script type='text/javascript'>

var map;

OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;

function init() {
    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);

    var MyKML = "http://www.xyz.com/tld/mypath/myfile.kml"

    map.addLayer(new OpenLayers.Layer.GML("KML", MyKML, 
           {
            format: OpenLayers.Format.KML, 
            formatOptions: {
              extractStyles: true, 
              extractAttributes: true,
              maxDepth: 1
            }
           }));

    // Adding controls to map  
    map.addControl(new OpenLayers.Control.ScaleLine());
    map.render('map_element');

    map.setCenter(
            new OpenLayers.LonLat(101.587008,3.021612).transform(
                new OpenLayers.Projection("EPSG:4326"),
                map.getProjectionObject()
            ), 17
        );      
}
</script>

The KML file could refresh automatically in Google Earth but not in OpenLayers. That is the real problem.

And one huge complexion is I cannot visualize the KML layer in OpenLayers. Either I use static or passive KML file is quite successful. Anybody?

Thanks in advance.

share|improve this question
    
I think it has been answered few days ago here. –  simo Jul 26 '11 at 7:16
    
To be honest, I had look into them a dozen of times. Unfortunately, no success result. Only this error: "host is not defined". How to define it? –  zearth Jul 26 '11 at 7:48
1  
var host = 'mydomain.tld/mypath' –  simo Jul 26 '11 at 8:31

3 Answers 3

You need to add a Refresh Strategy to your layer. There's also discussion about this on the osgeo forum.

share|improve this answer
    
thank you very much @vadim. –  zearth Aug 9 '11 at 4:26

You'll need a Javascript timer, I use the following to refresh WMS layers but it should work for KML layers too. The code below redraws all the visible overlay layers but you could replace loop with a single layer in a variable.

var timerId = 0;

var refresher = function(button,event){
  for(layer in mappanel.map.layers){
    l = mappanel.map.layers[layer];
    if(l.isBaseLayer===false && l.getVisibility()){
        l.redraw(true);
    }
  }
};

var setTimer= function(interval){
  if(interval <= 0){
     interval = 30;
  }
  refresher();
  timerId = setInterval("refresher()",interval*1000);
};

var clearTimer=function(){
  clearInterval(timerId);
}
share|improve this answer
    
I'm using function by the way. –  zearth Aug 9 '11 at 4:26
up vote 2 down vote accepted

I found the solution. Thank god.

var map;

OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;

function init() {
    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.addControl(new OpenLayers.Control.ScaleLine());
    map.render('map_element');
    map.zoomTo(5);  

    var osm = new OpenLayers.Layer.OSM();
    var vlayer = new OpenLayers.Layer.Vector( "Editable" );

    var MyKML = new OpenLayers.Layer.GML("KML", "myfile.kml",
            {
            projection: new OpenLayers.Projection("EPSG:4326"),
            //displayProjection: new OpenLayers.Projection("EPSG:900913"), 

            format: OpenLayers.Format.KML, 
            formatOptions: {
              extractStyles: true, 
              extractAttributes: true,
              maxDepth: 1
            }
           });

    map.addLayers([osm, vlayer, MyKML]);

    map.addControl(new OpenLayers.Control.PanZoomBar());
    map.setCenter(new OpenLayers.LonLat(101.587411,3.021547).transform(new OpenLayers.Projection ("EPSG:4326"),
    map.getProjectionObject()
    ), 
    15);

    window.setInterval(raingaugeRefreshData, 2000, MyKML);
}

function raingaugeRefreshData(layer){
            //setting loaded to false unloads the layer//
            layer.loaded = true;
            //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.setUrl('mydomain.tld/mypath/myfile.kml?_salt='+Math.random());        
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.