Search code examples
kmlgoogle-earth-plugin

Extracting Extended Data from KML using Javascript


I have a placemark in a KML that looks something like this

<Placemark> 
 <id>test345</id>
<name>Images from KML file</name>
<ExtendedData>
<Data name="type">
    <value>images</value>
</Data>
</ExtendedData>
<Point>
  <coordinates>-122.448425,37.802907,0</coordinates>
</Point>

I'm attempting to extract the ExtendedData information out of this placemarker on a click event:

google.earth.addEventListener(kmlObject, 'click', function(event) {
    event.preventDefault();
    var kmlPlacemark = event.getTarget();
});

An alternative solution would be to get the kmlObject from the kmlPlacemarker, any ideas?


Solution

  • Given the placemark the Google Earth API provides two methods to access the ExtendedData element.

    • getBalloonHtml()
    • getBalloonHtmlUnsafe()

    API Reference:
    https://developers.google.com/earth/documentation/reference/interface_kml_feature

    You can find a working example in the Google Code Playground here:
    https://code.google.com/apis/ajax/playground/?exp=earth#extended_data_in_balloons

    If you wanted to get the raw KML for extended data then you could fetch the KML representation and parse it as an XML document.

    var output = placemark.getKml();