有没有办法在添加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);
答案 0 :(得分:4)
由于Google Maps API将KML添加为DOM中的图片,因此您可以通过在<img>
属性中搜索包含"kml"
的{{1}}元素来使用CSS更改其不透明度:< / p>
src
此外,您可以使用jQuery实现此目的:
#map img[src*='kml'] {
opacity: .5;
}
但请记住,当用户缩放或移动地图时,将加载新的 KML图像,因此您必须再次调用该jQuery函数。 / p>
答案 1 :(得分:3)
您无法修改KmlLayer中多边形的不透明度。你有三个选择(我能想到):
example,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>
将KML导入FusionTables,然后使用FusionTablesLayer (which allows you to change the opacity of polygons) (不再有用,因为FusionTables将于2019年12月3日关闭/关闭)
如果KML不是太复杂,请使用第三方解析器(geoxml3或geoxml-v3,它将解析KML并将其渲染为原生Google Maps Javascript API v3对象(允许你修改不透明度。)
答案 2 :(得分:0)
此解决方案并不完美,因为它会在更改缩放级别时产生轻微闪光,但有些人可能会觉得这很有用:
google.maps.event.addListener(map, 'tilesloaded', function() {
$("#map").find("img").css("opacity","0.5");
});
答案 3 :(得分:0)
如果您使用preserveViewport: true
,则应该不会有任何问题。
var kmlLayer = new google.maps.KmlLayer(kmlsrc, {
suppressInfoWindows: true,
preserveViewport: true,
map: map
});