2016-11-10 103 views
-1

我已經使用Google Maps API創建了3個標記,但我只希望一個標記一次處於活動狀態,因爲每個標記都代表遊戲中的評分順序。例如,如果分數是0,我只希望芝加哥標記處於活動狀態。我迷失在如何做到這一點上,任何幫助都會很棒。下面是我的一些代碼:標記可見性Google Maps API

<script> 
     var map; 
     var score; 
     function initMap() { 
     var chicago = {lat: 41.8781, lng: -87.6298}; 
     var indianapolis = {lat: 39.7684, lng: -86.1581}; 
     var oklahomaCity = {lat: 35.4819, lng: -97.5084}; 

     map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat: 0.0, lng: 0.0}, 
      zoom: 1 
}); 
     var chicagoMarker = new google.maps.Marker({ 
      position: chicago, 
      map: map   

}); 

     var oklahomaCityMarker = new google.maps.Marker({ 
      position: oklahomaCity, 
      map:map 
}); 

     var indianapolisMarker = new google.maps.Marker({ 
      position: indianapolis, 
      map:map 

}); 


chicagoMarker.setVisible(false); 
indianapolisMarker.setVisible(false); 
oklahomaCityMarker.setVisible(false); 
    } 
    </script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCuvsCAF0gVmwv6AF0SoA3xBjV66RG4r7o&callback=initMap" 
    async defer></script> 

回答

2

創建標記withoou地圖屬性,並與的setMap(地圖)的assugn只有socre是0 VAR地圖;

var score; 
    function initMap() { 
     var chicago = {lat: 41.8781, lng: -87.6298}; 
     var indianapolis = {lat: 39.7684, lng: -86.1581}; 
     var oklahomaCity = {lat: 35.4819, lng: -97.5084}; 

     map = new google.maps.Map(document.getElementById('map'), { 
     center: {lat: 0.0, lng: 0.0}, 
     zoom: 1 
     }); 
     var chicagoMarker = new google.maps.Marker({ 
      position: chicago 
     }); 

     var oklahomaCityMarker = new google.maps.Marker({ 
      position: oklahomaCity 
     }); 

     var indianapolisMarker = new google.maps.Marker({ 
      position: indianapolis 
     }); 

     if (score == 0) { 
     chicagoMarker.setMap(map) 
     } 

    } 
</script>