5

添加时有没有办法设置kml图层的透明度?我正在添加一个您无法看到下面街道的 kml 图层。有没有办法做到这一点

 var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var ctaLayer = new google.maps.KmlLayer({
    url: 'images/test3.kml'
  });

  var ctaLayer2 = new google.maps.KmlLayer({
    url: 'images/test.kml'
  });
  var ctaLayer3 = new google.maps.KmlLayer({
    url: 'images/kmztest2.kmz'
  });
  ctaLayer3.setMap(map);
  ctaLayer2.setMap(map);
  ctaLayer.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);
4

4 回答 4

7

由于 KML 是由 Google Maps API 作为图像添加到 DOM 中的,因此您可以通过搜索其属性<img>中包含的元素来使用 CSS 更改其不透明度:"kml"src

#map img[src*='kml'] {
    opacity: .5;
}

此外,您可以使用 jQuery 实现此目的:

jQuery("#map").find("img[src*='kml']").css("opacity","0.5");

但请记住,当用户缩小或移动地图时,将加载新的KML 图像,因此您必须再次调用该 jQuery 函数。

于 2016-03-09T16:22:03.557 回答
4

您无法修改 KmlLayer 中多边形的不透明度。你有三个选择(我能想到的):

  1. 在 KML 中定义不透明度

例如,KML 如下所示:

<Style
  id="Style1">
  <LineStyle><color>66000001</color><width>1</width></LineStyle>
  <PolyStyle>
    <color>00ff6633</color>  <!-- first 00 is opacity of zero -->
    <fill>1</fill>
    <outline>1</outline>
  </PolyStyle>
</Style>
<Placemark id="Opacity0">
  <name>Opacity 0</name>
  <visibility>1</visibility><open>0</open><styleUrl>#Style1</styleUrl>
  <Polygon><extrude>0</extrude><tessellate>1</tessellate><altitudeMode>clampToGround</altitudeMode>
    <outerBoundaryIs><LinearRing><coordinates>-116.365673309192,43.6628960911185 -116.365591334179,43.6560111958534 -116.364375539124,43.6559975333512 -116.364402864128,43.6483204644173 -116.359539767727,43.6483955662698 -116.359567092732,43.64422573708 -116.356452545151,43.6442223004997 -116.356329582632,43.6403188481927 -116.355482675135,43.6384234484285 -116.354444492608,43.6376550793648 -116.354198567569,43.6375697515905 -116.354198567569,43.6375560890883 -116.354348855093,43.6375355534256 -116.352818906307,43.6375834140927 -116.349636046216,43.6375697515905 -116.349677033722,43.6339155770838 -116.317438473925,43.6339155770838 -116.314392238855,43.6339600011706 -116.314187301323,43.6484194546938 -116.334391040727,43.6484843306243 -116.33440470323,43.6627594660968 -116.335292598233,43.6629438679665 -116.336767980829,43.6629097536206 -116.359348576516,43.6629985179752 -116.360673587769,43.6628994438797 116.365673309192,43.6628960911185</coordinates>

 </LinearRing></outerBoundaryIs></Polygon>
 </Placemark>

结果地图的屏幕截图

  1. 将 KML 导入 FusionTables,并使用FusionTablesLayer(它允许您更改多边形的不透明度)(不再有用,因为 FusionTables 将于 2019 年 12 月 3 日关闭/关闭)

  2. 如果 KML 不太复杂,请使用第三方解析器(geoxml3geoxml-v3,它将解析 KML 并将其呈现为原生 Google Maps Javascript API v3 对象(允许您修改不透明度)。

于 2013-06-02T22:39:53.310 回答
1

这个解决方案并不完美,因为它在更改缩放级别时会导致轻微的闪光,但有些人可能会觉得这很有用:

google.maps.event.addListener(map, 'tilesloaded', function() {
            $("#map").find("img").css("opacity","0.5");
});
于 2017-05-25T04:00:45.740 回答
0

如果你使用preserveViewport: true,你不应该有任何问题。

var kmlLayer = new google.maps.KmlLayer(kmlsrc, {
      suppressInfoWindows: true,
      preserveViewport: true,
      map: map
    });
于 2020-03-18T19:21:55.480 回答