Search code examples
javascripthtmldomkmlgoogle-earth-plugin

Edit fetched KML element by id


Maybe someone has ideas why function getObjectById(id) does not work in Google earth? I have fetched kml to DOM, but then I try to found element with id it does return nothing.

I am playing in google earth api playground, maybe it does not support extra lybrary?

Here is my code:

<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 gets loaded, button works, but it seems like getObjectById("p"); does not work...


Solution

  • Firstly, I don't think there is a getObjectById(), you will most likely need to use either getElementByUrl() or getElementById()

    Since you are using fetchKml, I think you should use getElementByUrl() like this:

    var url = 'http://kml-samples.googlecode.com/svn/trunk/kml/NetworkLink/placemark.kml';
    var object = ge.getElementByUrl(url + '#' + id);
    

    See this page for details