我一直想在谷歌地图中切换 KML 图层。我已经将它设置为只显示一层而没有任何切换按钮。我在网上关注了很多例子,但似乎没有一个有效。到目前为止我设置的代码如下:
<html>
<head>
<script type="text/javascript">
var layers=[];
layers[0] = new google.maps.KmlLayer('http://wrf1.geology.um.maine.edu/bipush/kml/plot_5254.kml',
{preserveViewport: true});
layers[1] = new google.maps.KmlLayer('http://wrf1.geology.um.maine.edu/bipush/kml/plot_5938.kmz',
{preserveViewport: true});
var map;
function initialize() {
var latlng = new google.maps.LatLng(52.5028910, 13.41032740);
var myOptions = {
zoom: 3,
center: latlng,
mapTypeIds: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
}
function toggleLayers(i)
{
if(layers[i].getmap()==null){
kmlLayer01.setMap(null);
}
else{
layers[i].setMap(null);
}
}
</script> </head>
<body onload="initialize()">
Layer1 <input type="checkbox" id="layer_01" onclick="toggleLayers(0);"/>
Layer2 <input type="checkbox" id="layer_02" onclick="toggleLayers(1);"/>
<div id="map_canvas"></div>
</body>
</html>
请帮忙!