2010-06-08 92 views
5

TLDR我想在計時器刷新層,它繪製新的KML數據(如更新鏈接/網絡鏈路)刷新/重繪的OpenLayers層(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(); 

我本來以爲,這將刷新層。通過將其加載到false來卸載它。真正重新加載它的能力和數學隨機不應該允許它緩存?那麼有沒有人以前做過這個或知道我如何才能使這個工作?

回答

2

注意:雖然@Lavabeams方法工作得很好(我根據自己的需要調整了它,但完全沒有問題),kml圖層加載並不總是正確完成。

顯然,取決於您的動態kml需要解析多長時間,圖層刷新過程超時並考慮加載圖層。因此,在使用加載事件偵聽器(在將圖層添加到地圖之前)以及檢查實際加載的內容以及它是否符合期望值是明智的。

下面一個很簡單的檢查:

var urlKMLStops = 'parseKMLStops12k.php';   
var layerKMLStops = new OpenLayers.Layer.Vector("Stops", { 
      strategies: [new OpenLayers.Strategy.Fixed({ preload: true })], 
      protocol: new OpenLayers.Protocol.HTTP({ 
       url: urlKMLStops, 
       format: new OpenLayers.Format.KML({ 
        extractStyles: true, 
        extractAttributes: true, 
        maxDepth: 2 
       }) 
      }) 
     }); 

layerKMLStops.events.register("loadend", layerKMLStops, function() { 
       var objFs = layerKMLStops.features; 
       if (objFs.length > 0) { 
        alert ('loaded '+objFs.length+' '+objFs[0]+' '+objFs[1]+' '+objFs[2]); 
       } else { 
        alert ('not loaded'); 
        UpdateKmlLayer(layerKMLStops); 
       } 
      }); 

動態KML層提神,有時可能只得到部分結果,所以你可能還需要檢查是否裝載功能的數量等於特徵的預期數量。

警告詞:由於此偵聽器循環,請使用計數器來限制重新加載嘗試次數。

PS:你可能也想通過使層刷新異步任務:在上面的代碼

setTimeout(UpdateKmlLayer(layerKMLStops),0); 

最新的瀏覽器狀態:運作良好的鉻20.01132.47,而不是在Firefox 13.0.1,如果你同時調用各種函數(使用setTimeout加載多個動態kml圖層[track,stops,poi's])。

編輯:幾個月後,我對這個解決方案並不完全滿意。所以我creted 2中間步驟,保證我加載我的所有數據:

  1. 而不是直接拉動php文件,我使php KML分析器保存一個kml文件。然後我使用一個簡單的PHP閱讀器來閱讀這個文件。

爲什麼這個工作得更好:

等待PHP文件作爲一個KML層源解析,經常超時。但是,如果您將php分析器作爲ajax調用進行調用,則它將變爲同步的,並且您的代碼將等待php分析器完成其作業,然後才能刷新該圖層。

由於kml文件已被解析並保存,當我刷新時,我簡單的php閱讀器不超時。因爲你不必循環多次(你通常第一次成功),即使處理需要更長的時間,它會在第一時間完成任務(通常 - 我仍然檢查如果功能被加載)。

<?php 
session_start(); 

$buffer2 =""; 
// this is for /var/www/ztest 
// for production, use '../kmlStore 
$kmlFile = "fileVault/".session_id()."/parsedKML.kml"; 
//echo $kmlFile; 
$handle = @fopen($kmlFile, "r"); 
if ($handle) { 
    while (($buffer = fgets($handle, 4096)) !== false) { 
    $buffer2 .= $buffer; 
    } 
    echo $buffer2; 
    if (!feof($handle)) { 
    echo "Error: unexpected fgets() fail\n"; 
    } 
    fclose($handle); 
} 

?> 
+1

我的回答很老,但現在好多了。更新。 – Sphvn 2012-09-14 09:19:56

10

想通視爲這是很難足以讓我找到我想補充一點關於這個信息:)


創建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()} }); 
      } 

希望這可以讓其他人更容易。

+2

會很高興知道爲什麼這得到了-1。我問了一個問題,並在4天后自己找到了解決方案並更新了它?請解釋爲什麼幫助SO社區值得讚揚? – Sphvn 2010-08-23 04:57:29

+2

爲什麼不使用OpenLayers.Strategy.Refresh(http://dev.openlayers.org/apidocs/files/OpenLayers/Strategy/Refresh-js.html)而不是固定策略?它完全是爲你的目的而設計的。 – fbuchinger 2010-08-25 13:54:03

+1

我正在使用刷新..剩下的就是停止緩存問題橫跨Broswers IE/FF/Chrome/Safari/Opera ...並且讓它在一個函數中的原因是我可以多次調用多個圖層。使用visiblity真正的FORCES它重新加載,即使它決定在IE7中正常緩存它 - – Sphvn 2010-08-26 01:49:46