如何设置Google Maps图层中所有标记的标签颜色?

问题描述

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

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

这里提到了KML颜色代码帮助程序,但没有提供应用它的解决方案:Google Maps KML: 8-Digit Hex Code

KML参考建议使用<Style>,但不起作用:https://developers.google.com/kml/documentation/kmlreference#balloonstyle

是否可以为给定图层的所有标记设置颜色(通常是样式),以便将其与其他图层区分开?

解决方法

这不是完美的-我宁愿在UI中有一种简单的点击方法来执行此操作-可以了。

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

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

Screenshot of the UI showing the first several national parks; the markers are all the same blue. The first two are "Acadia" and "National Park of American Samoa".

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

The same first two map icons,but now the marker for "Acadia" is orange.

现在,将图层导出到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>

在这里,我们只需要用Acadia所使用的样式替换所有标签<styleUrl>中的引用样式。这是一个简单的查找和替换所有操作。我们还可以删除过时的<Style><StyleMap>,但这不是严格必须的。然后,保存更新的KML,然后将其重新导入到Google Maps中。

The same parks as in the first screenshot,but now ALL the markers are orange

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...