Search code examples
google-maps-api-3kmlgeoxml3

How to store extended data from parsed kml files into variables


I parse a kml file through geoxml3 where the placemarks have extended data fields. Using the geoxml.js from the kmz branch (trying to follow the solution here: Load kml extendeddata into variable with Geoxml3), I still find that geoXmlDoc.gpolylines[0].title works but geoXmlDoc.gpolylines[0].vars.val is undefined. Placemark formatting for reference:

<Placemark>
  <name>Left to Right</name>
  <description><![CDATA[One Way: True<br>Crossing: false<br>Closure: True]]></description>
  <styleUrl>#line-000000-1200</styleUrl>
  <ExtendedData>
    <Data name="One Way">
      <value>True</value>
    </Data>
    <Data name="Crossing">
      <value>0</value>
    </Data>
    <Data name="Closure">
      <value>True</value>
    </Data>
  </ExtendedData>
  <LineString>
    <tessellate>1</tessellate>
    <coordinates>
      115.8350241,-31.928985,0
      115.8409465,-31.9267996,0
      115.8470404,-31.9297863,0
      115.8533061,-31.9272367,0
    </coordinates>
  </LineString>
</Placemark>

Solution

  • I managed to do this by adding this last line in geoxml3's polyoptions variable (at line 1255).

    var polyOptions = geoXML3.combineOptions(parserOptions.polylineOptions, {
      map:           parserOptions.map,
      path:          path,
      strokeColor:   kmlStrokeColor.color,
      strokeWeight:  placemark.style.line.width,
      strokeOpacity: kmlStrokeColor.opacity,
      title:         placemark.name,
      visible:       placemark.visibility,
      polyDesc:      placemark.description,
      eData:         placemark.vars.val
    });
    

    Then I was able to access the variable with geoXmlDoc.gpolylines[x].eData['Closure'], where 'Closure' was the name of my extended data field.