在OpenLayers(KML)网络链接自动刷新中刷新/重绘图层 [英] Refresh / Redraw a Layer in OpenLayers (KML) Network-Link Auto Refresh

查看:720
本文介绍了在OpenLayers(KML)网络链接自动刷新中刷新/重绘图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR 我想在计时器上刷新一个图层,以便绘制新的kml数据(如更新链接/网络链接)

TLDR I want to refresh a layer on a timer so it plots the new kml data (like update-link / network link)

到目前为止,我已经尝试了以下动作功能:

                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
                   })
               })
           });

KMLLAYER的网址随机数学,因此它不会缓存:

var MYKMLURL = var currentanchorpositionurl = 'http://' + host + '/data?_salt=' + Math.random();

我原以为这会刷新图层。通过将其加载设置为false来卸载它。真实的可见性重新加载它和随机数学不应该允许它缓存?那么以前是否有人这样做过或者知道如何让它工作?

I would have thought that this would Refresh the layer. As by setting its loaded to false unloads it. Visibility to true reloads it and with the Math random shouldn't allow it to cache? So has anyone done this before or know how I can get this to work?

推荐答案

注意:虽然@Lavabeams的方法完美无缺好吧(我完全根据我的需要调整了它,没有任何问题),kml层加载并不总是正确完成。

ATTENTION: while @Lavabeams' method works perfectly well (i have adapted it to my needs with no problem at all), kml layer loading DOES NOT ALWAYS COMPLETE properly.

显然,根据动态kml解析的时间长短,图层刷新过程会超时并考虑加载的图层。

apparently, depending on how long your dynamic kml takes to parse, the layer refresh process times out and considers the layer loaded.

因此,明智的做法是使用加载事件监听器(在添加图层之前)并检查有效加载的内容以及是否符合预期。

therefore, it is wise to also use a loading event listener (before adding layer to map) and check what was effectively loaded and if it matches expectations.

下面是一个非常简单的检查:

below a very simple check:

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图层刷新,有时你可能只得到部分结果,所以你可能也想检查如果加载的要素数等于预期的要素数。

with dynamic kml layer refreshing, you might sometimes get only partial results, so you might want to ALSO check if the number of features loaded equals the expected number of features.

注意事项:由于此侦听器循环,请使用计数器来限制重新加载尝试次数。

words of caution: since this listener loops, use a counter to limit the number of reload attempts.

ps:您可能还希望通过使用以下方式使图层刷新成为异步任务:

ps: you might also want to make layer refresh an asynchronous task by using:

setTimeout(UpdateKmlLayer(layerKMLStops),0);

上述代码的最新浏览器状态:适用于chrome 20.01132.47,而不适用于firefox 13.0.1如果你同时使用setTimeout调用各种函数(加载多个动态kml图层[track,stops,poi])。

latest browser status on above code: works well on chrome 20.01132.47, not on firefox 13.0.1 if you simultaneously call various functions (to load multiple dynamic kml layers [track, stops, poi's]) using setTimeout.

编辑:几个月后,我对此并不完全满意这个解决方案所以我创建了2个中间步骤,保证我加载我的所有数据:

months later, i am not totally satisfied with this solution. so i creted 2 intermediate steps which guarantee that i load all my data:


  1. 而不是直接拉动php文件,我做了php kml解析器保存kml文​​件。然后我使用一个简单的PHP阅读器来阅读此文件。

为什么效果更好:

等待php文件解析作为kml图层的来源,经常超时。但是,如果你将php解析器称为ajax调用,它将变为同步,并且在继续刷新图层之前,您的代码会等待php解析器完成其工作。

waiting for the php file to parse as a source for a kml layer, often times out. BUT, if you call the php parser as an ajax call, it becomes synchronous and your code waits for the php parser to complete its job, before going on to refresh the layer.

由于我刷新时已经解析并保存了kml文件,我的简单php阅读器不会超时。

since the kml file has already been parsed and saved when i refresh, my simple php reader does not time out.

此外,因为您不必多次循环通过该层(您通常第一次成功),即使处理时间较长,它也可以获得第一次完成(通常 - 我仍然检查功能是否已加载)。

also, since you do not have to loop thru the layer as many times (you usually are successful the first time around), even though processing takes longer, it gets things done the first time around (usually - i still check if features were loaded).

<?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);
}

?>

这篇关于在OpenLayers(KML)网络链接自动刷新中刷新/重绘图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆