Accessing and altering objects in a loaded KML file (google earth plugin)

1.7k Views Asked by At

I am creating an example using the google earth browser plugin and API. I am trying to load a kml file using the fetchKml method

function addKmlFromUrl(kmlUrl) {
  google.earth.fetchKml(ge, kmlUrl, kmlFinishedLoading);
}

function kmlFinishedLoading(kmlObject) 
{
  if (kmlObject) {
    ge.getFeatures().appendChild(kmlObject);
  }
}

I am sucessfully loading the object, but once it is loaded, I want to be able to alter some of the parameters on the fly. I also want to be able to parse throug the points in it and do a few additional things. I looked through the documentation and code samples, but I can't seem to find a way to do this.

Any ideas?

1

There are 1 best solutions below

0
On

The kmlObject has a DOM just like XML (or any SGML language) you would need to recursively iterate over the elements in the DOM and then alter the ones which are applicable to you. Unfortunatly there are no direct methods in the api for this so you would either need to cook yor own or use a library the has the functions you need.

Something like the following might work for you.

function addKmlFromUrl(kmlUrl) {
  google.earth.fetchKml(ge, kmlUrl, kmlFinishedLoading);
}

function kmlFinishedLoading(kmlObject) 
{
  if (kmlObject) {
    ge.getFeatures().appendChild(kmlObject);
    traverseKml(kmlObject);
  }
}

function traverseKml(node) { 
  if(node.getFeatures().hasChildNodes()) { 

    var subNodes = node.getFeatures().getChildNodes(); 
    var length = subNodes.getLength(); 

    for(var i = 0; i < length; i++) { 

      var eachSubNode = subNodes.item(i); 
      var nodeType = eachSubNode.getType(); 

      switch(nodeType) { 
        // kml containers 
        case 'KmlFolder' : 
        case 'KmlDocument' :
          traverseKml(eachSubNode); 
        break; 
        // do something with all placemarks...
        case 'KmlPlacemark' : 
          alert(eachSubNode.getName()); 
        break; 
      } 
    } 
  } 
}