也许有人知道为什么函数getObjectById(id)在Google Earth中不起作用?我已经将kml提取到DOM,但是随后我尝试找到ID为id的元素,它什么也没有返回。

我在Google Earth API操场上玩,也许它不支持额外的库?

这是我的代码:

<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);


See this page for details

07-24 09:30