I have been tying to toggle KML layers in google maps. I have had it setup such that only one layer shows up with out any toggle button. I followed many examples online but none seem to work. The code I have setup so far is below:
<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>
Please help!
Ravin,
If you have two different maps, pass a variable for the map into the function, e.g
Lea.
the posted code does not include the API, this is missing:
There are a few typos:
kmlLayer01 is not defined, probably should be layers[i]
working example
code snippet: