5

我準備了一個簡化的測試用例和截圖。如何隱藏或顯示Google地圖圖層?

我想我錯過了一點點,只有幾行代碼。

我已經在我的JavaScript谷歌地圖2所覆蓋(在weather and clouds),並想隱藏或顯示他們在點擊相應的複選框時:

enter image description here

下面是測試情況下,只需粘貼它變成一個.html文件,它會運行:

<!DOCTYPE HTML> 
<html> 
<head> 
<style type="text/css"> 
    h1,p { 
     text-align: center; 
    } 

    #map { 
     width: 700px; 
     height: 400px; 
     margin-left: auto; 
     margin-right: auto; 
     background-color: #CCCCFF; 
    } 
</style> 
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false&language=de&libraries=weather"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script type="text/javascript"> 

$(function() { 
    findCity('Berlin'); 

    $('#weather_box,#clouds_box').click(function(){ 
     alert('How to hide/show layers? Checked: ' + $(this).is(':checked')); 
    }); 
}); 

function createMap(center) { 
    var opts = { 
     zoom: 6, 
     center: center, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    return new google.maps.Map(document.getElementById('map'), opts); 
} 

function findCity(city) { 
    var gc = new google.maps.Geocoder(); 
    gc.geocode({address: city}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      var pos = results[0].geometry.location; 
      var map = createMap(pos); 
      var marker = new google.maps.Marker({ 
       map: map, 
       title: city, 
       position: pos, 
       animation: google.maps.Animation.DROP 
      }); 
      var weatherLayer = new google.maps.weather.WeatherLayer({ 
       temperatureUnits: google.maps.weather.TemperatureUnit.CELSIUS 
      }); 
      weatherLayer.setMap(map); 
      //var cloudLayer = new google.maps.weather.CloudLayer(); 
      //cloudLayer.setMap(map); 
     } 
    }); 
} 
</script> 
</head> 
<body> 
<h1>Berlin</h1> 
<p>Show: 
<label><input type="checkbox" id="weather_box" checked>weather</label> 
<label><input type="checkbox" id="clouds_box">clouds</label> 
</p> 
<div id="map"></div> 
</body> 
</html> 

UPDATE:謝謝,在這裏工作的每個人都

版本
<!DOCTYPE HTML> 
<html> 
<head> 
<style type="text/css"> 
    h1,p { 
     text-align: center; 
    } 

    #map { 
     width: 700px; 
     height: 400px; 
     margin-left: auto; 
     margin-right: auto; 
     background-color: #CCCCFF; 
    } 
</style> 
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false&language=de&libraries=weather"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script type="text/javascript"> 

var map; 
var WeatherLayer; 
var CloudsLayer; 

$(function() { 
    findCity('Berlin'); 

}); 

function createMap(center) { 
    var opts = { 
     zoom: 6, 
     center: center, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    return new google.maps.Map(document.getElementById('map'), opts); 
} 

function findCity(city) { 
    var gc = new google.maps.Geocoder(); 
    gc.geocode({address: city}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      var pos = results[0].geometry.location; 
      map = createMap(pos); 
      var marker = new google.maps.Marker({ 
       map: map, 
       title: city, 
       position: pos, 
       animation: google.maps.Animation.DROP 
      }); 
      weatherLayer = new google.maps.weather.WeatherLayer({ 
       temperatureUnits: google.maps.weather.TemperatureUnit.CELSIUS 
      }); 
      weatherLayer.setMap(map); 
      cloudsLayer = new google.maps.weather.CloudLayer(); 
      //cloudsLayer.setMap(map); 

      $('#weather_box').click(function(){ 
       weatherLayer.setMap($(this).is(':checked') ? map : null); 
      }); 

      $('#clouds_box').click(function(){ 
       cloudsLayer.setMap($(this).is(':checked') ? map : null); 
      }); 

      $('#weather_box,#clouds_box').removeAttr('disabled'); 
     } 
    }); 
} 
</script> 
</head> 
<body> 
<h1>Berlin</h1> 
<p>Show: 
<label><input type="checkbox" id="weather_box" disabled="true" checked>weather</label> 
<label><input type="checkbox" id="clouds_box" disabled="true">clouds</label> 
</p> 
<div id="map"></div> 
</body> 
</html> 

回答

8

可以隱藏/與setMap方法展現層:

if ($(this).is(':checked')) 
    weatherLayer.setMap(map); // show 
else 
    weatherLayer.setMap(null); // hide 

見工作示例:http://jsfiddle.net/EeVUr/2/(刪除你的第二個複選框,因爲你現在只有一層。但是您可以輕鬆創建兩個不同的圖層並切換它們。)