Loading KML from String

I have a PHP script which brings back a KML document in string form using Ajax. My past workflow in Google Earth would include using parseKML to parse the text response. As I'm new to Cesium, I've been reading the KMLDataSource.js documentation in the cesium-kml branch, but I am a bit lost on what method I should use to add the KML to the map from a string. I've tried load() and loadKml(). I was wondering if anyone could point me in the right direction or let me know if I'm thinking about it completely wrong.

parametros = {
      "estado" : estado,
      "regcat" : regcat,
      "municipio" : municipio

      data: parametros,
      url: '../../Database/Municipios_sinKml.php',
      type: 'get',
      dataType: 'xml',
      cache: false,
      success: function (response) {
          var dataSource = new Cesium.KmlDataSource();

I took a look at the jQuery docs and dataSource.load(response) should be right, given the dataType ‘xml’, which seems like it should return a Document object parsed from the XML.

It could be that your KML file uses features that aren’t supported yet. The kml branch is still a work in progress.

Thanks a lot Scott for looking at this.

I modified my jQuery Ajax given your input and now it’s adding to the map via:

var kml = $.parseXML(response);

var dataSource = new Cesium.KmlDataSource();



Currently, Cesium is only loading the Placemark point though I would rather have the polygon (if loading mulitple geometries isn’t supported yet). Attached is a sample kml to see if there’s any further modifications that I might be able to do to make that happen.

I’ve tried removing the Placemark point and altitudeMode but no luck so far.

sample_kml.kml (43.2 KB)

Thanks for the sample, it helped pinpoint the problem. First, the ROJO style contains the following definitions for LineStyle and PolyStyle.

This is technically invalid KML and would not pass schema validation. Removing these lines makes it work. However, since this does work in Google Earth (and matching Google Earth is a big part of what we are trying to do), I’ve already submitted code to the kml branch to fix this. If you sync up, your file will work as-is.

Hope that helps,


Thanks Matt.

Hoping to get out of KML land soon to avoid future schema confusion…



Just wanted to say "THANK YOU VERY MUCH" for asking this question and finding the answer publically.

There is currently an exodus of developers from Google Earth since its deprecation was announced and those of us with applications that generate KML script dynamically really need a function like parseKML() in order to parse it after it's been loaded from an AJAX call.

The answer you gave worked perfectly for me.

For anyone else looking at this, the following code worked for me (the kmlString variable can be loaded with any valid KML script and it'll work)

      var viewer = new Cesium.Viewer('cesiumContainer');
      var kmlString = ''
              + '<?xml version="1.0" encoding="UTF-8"?>'
              + '<kml xmlns="http://www.opengis.net/kml/2.2">'

              + '<Document>'
              + ' <Camera>'
              + ' <longitude>-122.444633</longitude>'
              + ' <latitude>37.801899</latitude>'
              + ' <altitude>139.629438</altitude>'
              + ' <heading>-70.0</heading>'
              + ' <tilt>75</tilt>'
              + ' </Camera>'

              + ' <Placemark>'
              + ' <name>Placemark from KML string</name>'
              + ' <Point>'
              + ' <coordinates>-122.448425,37.802907,0</coordinates>'
              + ' </Point>'
              + ' </Placemark>'

              + '</Document>'
              + '</kml>';

      var kml = $.parseXML(kmlString);
      var dataSource = new Cesium.KmlDataSource();