0

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>
Boundless
  • 2,444
  • 2
  • 25
  • 40

1 Answers1

1

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>
CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
  • I read the documentation before posting my question. It seems overly limited to only allow variable substitution in balloons. I was hoping there was a way to use substitution for icons. It seems like it would be a basic feature. – Boundless Jul 12 '14 at 16:03
  • The entity substitution (e.g. $[name]) is restricted to only the text field of BalloonStyle or the description element itself. A more general substitution concept could be useful but was not defined in the KML standard. Such features can be submitted to the [OGC KML Standards Working Group](http://www.opengeospatial.org/projects/groups/kmlswg) which is currently finalizing KML 2.3. – CodeMonkey Jul 12 '14 at 22:34