Google Earth API Samples - Fetch KML (Interactive)

Last Modified:
08/04/08

Fetch KML at this URL:


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 {
    alert('Bad KML');
  }
}