有没有办法让谷歌地球插件在网页加载时执行以下操作?
- 显示静态 .kmz 文件,如http://www.ppacg.org/tours/logo.html
- 启动巡回播放器.kmz,如http://www.ppacg.org/tours/tabview.html?project=08-37
我可以分别执行上面的#1 或#2,但我不知道如何让它们在网页加载时同时发生。
有没有办法让谷歌地球插件在网页加载时执行以下操作?
我可以分别执行上面的#1 或#2,但我不知道如何让它们在网页加载时同时发生。
您可以使用google.earth 命名空间中的fetchKml方法简单地加载这两个文件。然后,您可以提供逻辑来处理显示数据并在回调参数中输入游览。
要播放游览,您必须在 Kml DOM 中寻找KmlTour对象,以便您可以使用GETourPlayer打开它。为此,您可以使用earth 实用程序库,或者您可以使用kmldomwalk.js脚本。
类似以下 java 脚本的东西应该可以工作(尽管它是在这里编写的并且未经测试)。
<script src="//www.google.com/jsapi/"></script>
<script src="//earth-api-samples.googlecode.com/svn/trunk/lib/kmldomwalk.js"></script>
<script>
google.load("earth", "1");
var ge = null;
var kml1= '//www.ppacg.org/tours/logo.html';
var kml2= '//www.ppacg.org/tours/tabview.html?project=08-37';
var tour = null; // so you can call pause, stop, etc globally...
function init() {
// presumes you have a div with the id 'map3d'
google.earth.createInstance("map3d", initCallback, function(e){alert(e);});
}
function initCallback(object) {
ge = object;
ge.getWindow().setVisibility(true);
// load your data
google.earth.fetchKml(ge, kml1, fetchKmlCallback);
google.earth.fetchKml(ge, kml2 , fetchKmlCallback);
}
function fetchKmlCallback(object) {
if (object) {
// add the features to the plugin
ge.getFeatures().appendChild(object);
// Walk the DOM looking for a KmlTour
walkKmlDom(object, function() {
if (this.getType() == 'KmlTour') {
tour = this;
ge.getTourPlayer().setTour(tour); // enter the tour
return false; // stop the DOM walk here.
}
});
} else {
setTimeout(function() {
alert('Bad or null KML.');
}, 0);
}
}
google.setOnLoadCallback(init);
</script>