Google Earth API Samples - Fetch KML (Interactive)

Last Modified:
06/08/2009
Installed Plugin Version:
...

Fetch KML at this URL:


Relevant Resources:

Relevant Code Excerpt:

var currentKmlObject = null;

function fetchKmlFromInput() {
  // remove the old KML object if it exists
  if (currentKmlObject) {
    ge.getFeatures().removeChild(currentKmlObject);
    currentKmlObject = null;
  }
  
  var kmlUrlBox = document.getElementById('kml-url');
  var kmlUrl = kmlUrlBox.value;
  
  google.earth.fetchKml(ge, kmlUrl, finishFetchKml);
}

function finishFetchKml(kmlObject) {
  // check if the KML was fetched properly
  if (kmlObject) {
    // add the fetched KML to Earth
    currentKmlObject = kmlObject;
    ge.getFeatures().appendChild(currentKmlObject);
  } else {
    // wrap alerts in API callbacks and event handlers
    // in a setTimeout to prevent deadlock in some browsers
    setTimeout(function() {
      alert('Bad or null KML.');
    }, 0);
  }
}