5

간단한 테스트 케이스와 스크린 샷을 준비했습니다.Google지도 레이어를 숨기거나 표시하는 방법은 무엇인가요?

저는 코드가 몇 줄 밖에없는 것 같아요.

내가 내 자바 스크립트 구글지도에서 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/은 (두 번째 체크 박스를 제거했습니다. 그러나 쉽게 두 개의 다른 레이어를 만들고 전환 할 수 있습니다.)

관련 문제