【问题标题】:Edit fetched KML element by id按 id 编辑获取的 KML 元素
【发布时间】:2012-12-02 19:17:15
【问题描述】:

也许有人知道为什么函数 getObjectById(id) 在 Google 地球中不起作用?我已将 kml 提取到 DOM,但随后我尝试找到具有 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");不工作...

【问题讨论】:

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


    【解决方案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

    【讨论】:

    • 我是按照这个来做的:Accessing Google Earth DOM
    • 您是否正确加载了 gex?即在您的 init(instance) 函数中,您首先加载 ge,然后加载 gex:ge = instance;gex = new GEarthExtensions(ge);
    • 为什么不直接使用我在回答中提到的功能 - 默认插件支持它们,因此无需加载扩展(除非您在其他地方使用它们)
    • 我正在研究它,也许有一种方法可以显示所选项目,例如“alert(object)”?我找不到检查它是否在我的应用程序上运行的方法,因为服务器是 Ubuntu :)
    • 它成功了 :) 也许你知道一种快速设置 fethed kml 视图的方法?
    猜你喜欢
    • 2010-12-11
    • 2011-03-25
    • 2020-10-12
    • 2017-06-14
    • 2020-09-20
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多