1

I'm trying to generate a custom map on Google's MyMaps using a KML file exported from R. I'm expecting a chloropleth map - a series of polygons with their colour based on a variable.

I've pasted a simplified structure of the KML file below - there are actually 98 polygons in the actual file, each with their own Style id.

My question is why, when I import this file as a layer in MyMaps, the colour of the polygons are all shown as black? I would've thought that, since they all have a `PolyStyle' tag with a 'color' value, that they should all be displayed with separate colours. I've tried clearing the cache for MyMaps but that hasn't seemed to do anything.

<kml xmlns:xsd="https://schemas.opengis.net/kml/2.2.0/ogckml22.xsd" xmlns:xmlns="https://www.opengis.net/kml/2.2/" version="1.0">
  <Document>
    <name>map_test2_1</name>
    <open>1</open>
    <Folder>
      <name>sfdata.frame</name>
      <Style id="poly1">
        <PolyStyle>
          <color>#ff445eb5</color>
          <outline>1</outline>
          <fill>1</fill>
        </PolyStyle>
        <BalloonStyle>
          <text>$[description]</text>
        </BalloonStyle>
      </Style>
     <Placemark>
        <name>School Lane/Tankerton Road Area</name>
        <styleUrl>#poly1</styleUrl>
        <Polygon>
          <extrude>1</extrude>
          <tessellate>0</tessellate>
          <altitudeMode>clampToGround</altitudeMode>
          <outerBoundaryIs>
            <LinearRing>
              <coordinates>-0.3060805,51.402915,0
 -0.2748381,51.3823493,0
 -0.2552687,51.4096612,0
 -0.3060805,51.402915,0</coordinates>
            </LinearRing>
          </outerBoundaryIs>
        </Polygon>
      </Placemark>
  </Folder>
  </Document>
</kml>
0

2 Answers 2

1

Your PolyStyles look like this:

<PolyStyle>
  <color>#ff445eb5</color>
  <outline>1</outline>
  <fill>1</fill>
</PolyStyle>

I just tried exporting a MyMap as KML and got this:

  <PolyStyle>
    <color>4d4f0e88</color>
    <fill>1</fill>
    <outline>1</outline>
  </PolyStyle>

Subtle difference : there's no # character before the colour code. This might be the problem.

1

Looks like your KML has several potential issues.

Location of shared Style - The key issue that appears to be blocking your color in Google My Maps is that the shared Style is inside the Folder level. If you move the shared Style up to the Document level, then it works. When I make only that change and import to My Maps, the polygon is colored (solid brown). It should also work if you put the Style directly in the Placemark, but then you can't share it with other similarly styled placemarks.

KML color encoding - You probably also have an issue with the way the color is defined, since I'm guessing you intended it to be semi-transparent pink, not the solid brown I'm seeing. As @Spacedman pointed out in his answer, you have a "#" at the beginning of your color, which is the HTML convention and needs to be removed. Some KML renderers will ignore the "#", but it's best to remove it. More importantly, KML colors are encoded backwards from the HTML convention. HTML colors are "#rrggbbaa": Red Green Blue Alpha, while KML colors are "AABBGGRR": Alpha Blue Green Red. The Alpha/opacity is in front, and the order is BGR instead of the more common RGB. So when I flip your color around to be: <color>b55e44ff</color>, then it shows up as semi-transparent pink.

Vertex Winding Order - It looks like this is NOT an issue in your KML, but for others trying to figure out why polygons aren't showing up correctly, it's something to double-check. KML polygons need the vertices to be listed in counter-clockwise order. If the vertices wind clockwise (backwards), then the polygon can be rendered "upside-down", with the darker, shaded underside showing, instead of the intended color & lighting. I'm not sure if that happens in My Maps but it can be an issue with elevated polygons in Google Earth.

XML & KML Headers - Again, this isn't breaking your colors since My Maps and Earth are pretty forgiving about KML formatting, but it's best practice to do it right and maximize compatibility with more KML clients & renderers. You're missing the XML declaration at the top of the file, and your KML declarations are incomplete. Best practice is to include lines like the two below at the top of your KML file:

<?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" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">

Or at least this bare minimum:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">

Finally... below is a working KML with the 3 recommended corrections above. Good luck!

<?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" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
  <Document>
    <name>map_test2_1</name>
    <open>1</open>
    <Style id="poly1">
      <PolyStyle>
        <color>b55e44ff</color>
        <outline>1</outline>
        <fill>1</fill>
      </PolyStyle>
      <BalloonStyle>
        <text>$[description]</text>
      </BalloonStyle>
    </Style>
    <Folder>
      <name>sfdata.frame</name>
     <Placemark>
        <name>School Lane/Tankerton Road Area</name>
        <styleUrl>#poly1</styleUrl>
        <Polygon>
          <extrude>1</extrude>
          <tessellate>0</tessellate>
          <altitudeMode>clampToGround</altitudeMode>
          <outerBoundaryIs>
            <LinearRing>
              <coordinates>
                -0.3060805,51.402915,0
                -0.2748381,51.3823493,0
                -0.2552687,51.4096612,0
                -0.3060805,51.402915,0
              </coordinates>
            </LinearRing>
          </outerBoundaryIs>
        </Polygon>
      </Placemark>
  </Folder>
  </Document>
</kml>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.