按ID编辑获取的KML元素

时间:2012-12-02 19:17:15

标签: javascript html dom kml google-earth-plugin

也许有人有想法为什么函数getObjectById(id)在Google地球上不起作用?我已经获取了kml到DOM,但后来我尝试找到带有id的元素它确实没有返回任何内容。

我正在谷歌地球api游乐场玩,也许它不支持额外的lybrary?

这是我的代码:

<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”);不起作用......

1 个答案:

答案 0 :(得分:1)

首先,我认为没有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

相关问题