Sph*_*hvn 5 javascript kml openlayers
TLDR我想在计时器上刷新一层,以便绘制新的kml数据(如更新链接/网络链接)
到目前为止,我已经尝试了如下动作功能:
function RefreshKMLData(layer) {
layer.loaded = false;
layer.setVisibility(true);
layer.redraw({ force: true });
}
Run Code Online (Sandbox Code Playgroud)
设置功能的间隔:
window.setInterval(RefreshKMLData, 5000, KMLLAYER);
Run Code Online (Sandbox Code Playgroud)
图层本身:
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
})
})
});
Run Code Online (Sandbox Code Playgroud)
KMLLAYER的url随机数学,因此它不会缓存:
var MYKMLURL = var currentanchorpositionurl = 'http://' + host + '/data?_salt=' + Math.random();
Run Code Online (Sandbox Code Playgroud)
我原本以为这会刷新图层.通过将其加载设置为false来卸载它.真实的可见性重新加载它和随机数学不应该允许它缓存?那么以前是否有人这样做过或者知道如何让它发挥作用?
Sph*_*hvn 11
想看,因为我很难找到有关这方面的信息,我想补充一下:
1)
创建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
})
})
});
Run Code Online (Sandbox Code Playgroud)
2)
设置KML图层的URL:
//note that I have host equal to location// //Math.Random will stop caching//
var mykmlurl = 'http://' + host + '/KML?key=' + Math.random();
Run Code Online (Sandbox Code Playgroud)
3)
设置刷新图层的时间间隔:
//function called// //timer// //layer to refresh//
window.setInterval(UpdateKmlLayer, 5000, MyKmlLayer);
Run Code Online (Sandbox Code Playgroud)
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()} });
}
Run Code Online (Sandbox Code Playgroud)
希望这会让其他人更容易.
注意:虽然 @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);
}
});
Run Code Online (Sandbox Code Playgroud)
通过动态 KML 层刷新,您有时可能只能得到部分结果,因此您可能还需要检查加载的要素数量是否等于预期的要素数量。
警告:由于此侦听器会循环,因此请使用计数器来限制重新加载尝试的次数。
ps:您可能还想使用以下方法使图层刷新成为异步任务:
setTimeout(UpdateKmlLayer(layerKMLStops),0);
Run Code Online (Sandbox Code Playgroud)
上述代码的最新浏览器状态:如果您使用 setTimeout 同时调用各种函数(加载多个动态 kml 图层 [track、stops、poi's]),则在 chrome 20.01132.47 上运行良好,但在 firefox 13.0.1 上运行不佳。
编辑:几个月后,我对这个解决方案并不完全满意。所以我创建了两个中间步骤来保证我加载所有数据:
为什么这样效果更好:
等待 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);
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12740 次 |
| 最近记录: |