首页 > 解决方案 > 如何为谷歌地图图层中的所有标记设置标签颜色?

问题描述

我看过一些相关的问题,但没有一个完全回答我的问题。

这提到了 Javascript: How to override KML colors in Google Map?

这提到了 KML 颜色代码助手,但没有给出应用它的解决方案:Google Maps KML: 8-Digit Hex Code

KML 参考建议使用 a<Style>但没有用:https ://developers.google.com/kml/documentation/kmlreference#balloonstyle

有没有办法为给定图层的所有标记设置颜色(或样式),以便将其与其他图层区分开来?

标签: google-mapskml

解决方案


这并不完美——我宁愿在 UI 中有一种简单的点击式方法来执行此操作——但是就这样吧。

让我们以美国国家公园的一层标记为例;取自维基百科:https ://tools.wmflabs.org/kmlexport?article=List_of_national_parks_of_the_United_States

我已使用导入工具将这些添加到新地图中

显示前几个国家公园的 UI 截图; 标记都是相同的蓝色。 前两个是

我认为最简单的方法是将所有所需的样式应用于 UI 中的单个标记,例如第一个标记。为简单起见,我将其设为橙色:

前两个地图图标相同,但现在标记为

现在,将图层导出到 KML 并打开 KML 文件。这是该文件的压缩版本:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>National parks</name>
    <Style id="icon-1899-0288D1-normal">
      <IconStyle>
        <color>ffd18802</color>
        <!-- more attributes -->
      </IconStyle>
      <LabelStyle>
        <scale>0</scale>
      </LabelStyle>
    </Style>
    <Style id="icon-1899-0288D1-highlight">
       <!-- similar markup -->
    </Style>
    <StyleMap id="icon-1899-0288D1">
      <Pair>
        <key>normal</key>
        <styleUrl>#icon-1899-0288D1-normal</styleUrl>
      </Pair>
      <Pair>
        <key>highlight</key>
        <styleUrl>#icon-1899-0288D1-highlight</styleUrl>
      </Pair>
    </StyleMap>
    <!-- Two more <Style> tags for -normal and -highlight -->
    <Style id="icon-1899-F57C00-normal"></Style>
    <Style id="icon-1899-F57C00-highlight"></Style>
    <!-- Another <StyleMap> -->
    <StyleMap id="icon-1899-F57C00"></StyleMap>
    <Placemark>
      <name>Acadia</name>
      <description>...</description>
      <styleUrl>#icon-1899-F57C00</styleUrl>
      <Point>...</Point>
    </Placemark>
    <Placemark>
      <name>National Park of American Samoa</name>
      <description>...</description>
      <styleUrl>#icon-1899-0288D1</styleUrl>
      <Point></Point>
    </Placemark>
    <!-- More <Placemark>s for the other parks -->
  </Document>
</kml>

从这里开始,我们只需将<styleUrl>所有标签中的参考样式替换为 Acadia 使用的样式即可。这是一个简单的查找和替换。我们也可以删除过时<Style>的 s,<StyleMap>但这并不是绝对必要的。之后,保存更新的 KML 并将其重新导入 Google 地图,我们就完成了:

与第一个屏幕截图中相同的公园,但现在所有标记均为橙色


推荐阅读