Google Earth fetchKml超时 [英] google earth fetchKml timeout
问题描述
我正在通过javascript调用goolge Earth api函数'fetchKml'.当获取大型文件时,firefox给我一个弹出窗口,显示此页面上的脚本可能很忙,或者它可能已停止响应.您可以立即停止脚本,在调试器中打开脚本,或者继续执行脚本."
I am calling the goolge earth api function 'fetchKml' via javascript. When fetching large files firefox gives me a popup that says "A script on this page may be busy, or it may have stopped responding. You can stop the script now, open the script in the debugger, or let the script continue."
我在Google网上论坛第331版上注意到了一个类似的问题("fetchKml无法在速度较慢的连接或快速连接以及较大的KML/KMZ文件上失败").好了-这个问题发生在2009年.现在是2012年.如何以编程方式在不发生超时问题的情况下加载文件?
I noticed a similar question on google groups issue 331 ('fetchKml fails on slower connections or fast connections and large KML/KMZ files'). Well alas - that issue was in 2009. Now is 2012. How do I progromatically get the file to load without getting the timeout issue?
谢谢.
推荐答案
The root cause is that fetchKml is synchronous. It will block until the KML file is fetched over the network, parsed, and the content it specifies is loaded in the plugin. If any of these steps take too long (the server is slow to respond, the file is very large, the content is overly complicated) and so the function call takes too long, it will trip the slow script warning in browsers, which usually just triggers on a simple time-based watchdog loop.
有几种方法可以解决此问题:
There are a few ways to work around this:
-
最简单的方法是将您的内容分成几个单独的部分 KML文件并加载每个文件,也许在
setTimeout
在每个fetchKml()
调用之间.如果您的主要问题是这将无济于事 延迟,但是由于您说过这种情况发生在大型KML文件中,
The simplest way is to split your content into several separate KML files and load each of them, maybe yielding with a
setTimeout
in between eachfetchKml()
call. This won't help if your main problem is latency, but since you said this was happening with large KML files, that probably isn't the case here.
惯用的方法是在KML中使用 NetworkLinks 来加载
这些其他文件.第一个fetchKml
调用将是同步的,但是
然后(取决于更新机制的设置方式)
内容将由插件自行加载,而无需
阻止JavaScript执行线程.
The idiomatic way is to use NetworkLinks in your KML to load
these other files. The first fetchKml
call will be synchronous, but
then (depending on how the update mechanism is set) subsequent
content will be loaded by the plugin in its own time, without
blocking the javascript execution thread.
最后,这是您自己掌握的方法.你可以
使用浏览器的本机XMLHttpRequest
功能加载KML
文件,默认情况下是异步的.加载文件后,您的
回调函数将被通知,然后就可以使用
parseKml()将该文件加载到插件中并显示其内容
内容.请注意,parseKml
也是同步的,因此它将阻塞
在解析文件和创建内容时,但通常
与在以下位置下载文件相比,这是一个非常快的步骤
第一名.
Finally, there is the take-matters-into-your-own-hand way. You can
use the browsers' native XMLHttpRequest
functionality to load the KML
file, which is asynchronous by default. When the file is loaded, your
callback function will be notified, and then you can use
parseKml() to load that file in the plugin and display its
content. Note that parseKml
is also synchronous, so it will block
while the file is being parsed and the content created, but usually
that is a very fast step compared to downloading the file in the
first place.
这篇关于Google Earth fetchKml超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!