percent-encoded URLs are mangled by Google Earth

2020-04-15 11:01发布

问题:

In a KML file for Google Earth consumption I am using Google Charts dynamic icons whose URLs contain percent-encode characters, e.g., this one. As can be seen by intercepting network calls, the %E2%80%A2 (bullet character) is mangled by Google Earth into %C3%A2%C2%80%C2%A2, which causes the icon retrieval to fail. The problem is that the KML spec is extremely vague: of the IconStyle Icon href element it will only say that it is "an HTTP address [...] used to load an icon". So, can any Googler clarify what Google Earth expects and how to make icon URLs in KML files with percent-encoded characters work properly?

Please do not give me grief about how maybe the URL above is somehow incorrect: it works fine in a browser (after replacing  with the ampersand) and there is an example just like it about halfway through the dynamic icons developer reference.

An actual KML example file follows:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
  <Folder>
    <Placemark>
      <Style>
        <IconStyle>
          <scale>1.6</scale>
          <Icon>
            <!-- doesn't work -->
            <href>http://chart.apis.google.com/chart?chst=d_map_pin_letter_withshadow&#x26;chld=%E2%80%A2|cccccc|000000</href>
          </Icon>
        </IconStyle>
      </Style>
      <Point>
        <coordinates>-3.67,40.51</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <Style>
        <IconStyle>
          <scale>1.6</scale>
          <Icon>
            <!-- works -->
            <href>http://chart.apis.google.com/chart?chst=d_map_pin_letter_withshadow&#x26;chld=O|cccccc|000000</href>
          </Icon>
        </IconStyle>
      </Style>
      <Point>
        <coordinates>-3.68,40.52</coordinates>
      </Point>
    </Placemark>
  </Folder>
</Document>
</kml>

回答1:

I came back to this after a long lull and found the answer. Even though you are inserting an URL and thus URL encoding guidelines should apply KML expects special entities to be Unicode- and not URL-encoded even in URLs! In other words you need this:

<href>http://chart.apis.google.com/chart?chst=d_map_pin_letter_withshadow&#x26;chld=&#x2022;|cccccc|000000</href>

In retrospect the fact that that it requires "&#26;" for the ampersand should have put me on the right track but hindsight is always 20/20...