您可以使用google.earth namespace 中的fetchKml 方法简单地加载这两个文件。然后,您可以提供逻辑来处理显示数据并在回调参数中输入游览。
要玩游览,您必须在 Kml DOM 中寻找KmlTour 对象,以便您可以使用GETourPlayer 打开它。为此,您可以使用earth utility library,或者您也可以使用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>
如果遇到困难,还可以看看这些使用 fetchkml 和 playing tours 的示例。