Search code examples
kmlgoogle-earthvariable-substitution

Variable Substitution in KML Icon Reference


How can I do variable substitution in a kml icon reference? I'm using Google Earth to load the kml, and my image doesn't appear for this simple example:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Document>
      <name>TestMap</name>
      <Style id="Icon1">
          <IconStyle>
            <Icon>
              <href>$[url]</href>
            </Icon>
          </IconStyle>
      </Style>
      <Placemark> 
        <name>Hello World</name>
        <styleUrl>#Icon1</styleUrl>
        <ExtendedData>
            <Data name="url"> 
                <value>http://magiccards.info/scans/en/al/232.jpg</value>
            </Data>
        </ExtendedData>
        <Point>
          <coordinates>
            0,0,0
          </coordinates>
        </Point>
      </Placemark>
    </Document>
  </Document>
</kml>

Solution

  • Variable substitution for extended data in KML only works in context of the description so you could show the placemark's data url via the description balloon.

    <kml xmlns="http://www.opengis.net/kml/2.2">
    <Document>
      <name>Data+BalloonStyle</name>
      <Style id="balloon-style">
        <BalloonStyle>
          <text>
            <![CDATA[
                $[name]<br>
                <img src="$[url]"/>
            ]]>
          </text>
        </BalloonStyle>
      </Style>
      <Placemark>
        <name>Hello World</name>
        <styleUrl>#balloon-style</styleUrl>
        <ExtendedData>
            <Data name="url"> 
                <value>http://magiccards.info/scans/en/al/232.jpg</value>
            </Data>
        </ExtendedData>
        <Point>
          <coordinates>-111.956,33.5043</coordinates>
        </Point>
      </Placemark>
    </Document>
    </kml>
    

    See related tutorial for adding custom data which describes using the BalloonStyle Element as a Template
    https://developers.google.com/kml/documentation/extendeddata

    If you want to display a custom icon via IconStyle per placemark then you need to define an inline Style for each placemark with the appropriate URL.

      <Placemark>
        <name>Hello World</name>
        <Style>
          <IconStyle>
            <Icon>
                <href>http://magiccards.info/scans/en/al/232.jpg</href>
            </Icon>
        </IconStyle>
        </Style>
        <Point>
          <coordinates>-111.956,33.5043</coordinates>
        </Point>
      </Placemark>