也许有人知道为什么函数 getObjectById(id) 在 Google 地球中不起作用?我已将 kml 提取到 DOM,但随后我尝试找到带有 id 的元素,但它没有返回任何内容。
我正在 google Earth api Playground 中玩,也许它不支持额外的库?
这是我的代码:
<script src="http://www.google.com/jsapi?key=ABQIAAAAuPsJpk3MBtDpJ4G8cqBnjRRaGTYH6UMl8mADNa0YKuWNNa8VNxQCzVBXTx2DYyXGsTOxpWhvIG7Djw" type="text/javascript"></script>
<script src="http://earth-api-utility-library.googlecode.com/svn/tags/extensions-0.2.1/dist/extensions.pack.js" type="text/javascript"></script>
<script type="text/javascript">
................
// fetch the KML
var url = 'http://kml-samples.googlecode.com/svn/trunk/kml/NetworkLink/placemark.kml';
google.earth.fetchKml(ge, url, finished);
}
function failureCallback(errorCode) {
}
function getObjectById(id) {
var foundObject = null;
// Traverse the DOM, looking for the object with the given ID.
gex.dom.walk({
rootObject: ge,
visitCallback: function() {
if ('getId' in this && this.getId() == id) {
foundObject = this;
return false; // Stop the walk.
}
}
});
return foundObject;
}
function buttonClick() {
getObjectById("p");
alert("Found:" +foundObject);
}
KML 已加载,按钮可以工作,但看起来像 getObjectById("p");不起作用...
最佳答案
首先,我认为没有 getObjectById()
,您很可能需要使用 getElementByUrl()
或 getElementById()
由于您使用的是 fetchKml,我认为您应该像这样使用 getElementByUrl()
:
var url = 'http://kml-samples.googlecode.com/svn/trunk/kml/NetworkLink/placemark.kml';
var object = ge.getElementByUrl(url + '#' + id);
关于javascript - 按 id 编辑获取的 KML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13672782/