google earth fetchKml timeout

假如想象 提交于 2019-12-11 04:08:26

问题


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."

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?

Thank you.


回答1:


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:

  • 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 each fetchKml() 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.

  • 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.

  • 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.



来源:https://stackoverflow.com/questions/11562694/google-earth-fetchkml-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!