I'm trying to add toggeble traffic, cloud and weather layers to Google maps using check boxes. However when I try to do so all of the layers disappear no matter if the boxes were checked or unchecked. I've 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 will 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>
modified "check" function:
function check()
{
if(document.getElementById('weather').checked)
{weatherLayer.setMap(map);}
else
{weatherLayer.setMap(null);}
if(document.getElementById('clouds').checked)
{cloudLayer.setMap(map);}
else
{cloudLayer.setMap(null);}
if(document.getElementById('traffic').checked)
{trafficLayer.setMap(map);}
else
{trafficLayer.setMap(null);}
}
working example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With