Search code examples
google-mapsgoogle-maps-markerskml

KML: Put the icon in the placemark rather than Style


I'm making a KML file with several placemarks, with several different icons. Is there a way to put the icon URL in the placemark, rather than having to have lots of different styles, most only used once?

The normal syntax I use is:

  <Document>
    <Style id="Point">
      <IconStyle>
        <Icon>
          <href>http://maps.google.com/mapfiles/kml/pushpin/blue-pushpin.png</href>
        </Icon>
      </IconStyle>
    </Style>

    <Placemark>
      <styleUrl>#Point</styleUrl>
      <name>1</name>
      <Point>
        <coordinates>-1.1234567,52.123456,0</coordinates>
      </Point>
    </Placemark>
  </Document>

And I've tried:

  <Document>
    <Placemark>
      <IconStyle>
        <Icon>
          <href>http://maps.google.com/mapfiles/kml/pushpin/blue-pushpin.png</href>
        </Icon>
      </IconStyle>
      <name>1</name>
      <Point>
        <coordinates>-1.8123456,52.523456,0</coordinates>
      </Point>
    </Placemark>
  </Document>

But it doesn't work.


Solution

  • That isn't valid KML. Per the documentation a Placemark can contain

    or

    A valid <StyleSelector> would be <Style>:

    <Style id="ID">
    <!-- extends StyleSelector -->
    
    <!-- specific to Style -->
      <IconStyle>...</IconStyle>
    </Style>
    

    Which contains the <IconStyle>.

    So your KML should be:

    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
      <Document>
        <Placemark>
          <Style>
            <IconStyle>
              <Icon>
                <href>http://maps.google.com/mapfiles/kml/pushpin/blue-pushpin.png</href>
              </Icon>
            </IconStyle>
          </Style>
          <name>1</name>
          <Point>
            <coordinates>-1.8123456,52.523456,0</coordinates>
          </Point>
        </Placemark>
      </Document>
    </kml>