I am trying to add toggeble traffic, cloud and weather layers to Google maps using checkboxes. However, when I try to do this, all the layers disappear regardless of whether the checkboxes have been checked or unchecked. I have never done anything like this in Javascript, and I'm really new to Javascript, so I have no idea what I'm doing wrong. Here is my code, any help would be great!
Javascript:
function check()
{
var weatherLayer = new google.maps.weather.WeatherLayer({
temperatureUnits: google.maps.weather.TemperatureUnit.FAHRENHEIT
});
var trafficLayer = new google.maps.TrafficLayer();
var cloudLayer = new google.maps.weather.CloudLayer();
if(document.getElementById('weather').checked)
{weatherLayer.setMap(map);}
else if(!document.getElementById('weather').checked)
{weatherLayer.setMap(map);}
cloudLayer.setMap(map);
trafficLayer.setMap(map);
}
HTML
<label><input type="checkbox" id="weather" checked="checked" onclick="check()" />Weather</label>
<label><input type="checkbox" id="clouds" onclick="check()" />Clouds</label>
<label><input type="checkbox" id="traffic" onclick="check()" />Traffic</label>
kduan source
share