2012-10-17 78 views
0

我是Google地圖的新手。我將Google Maps v3地圖添加到ASP.NET 4.0項目的aspx文件中,並試圖爲我在地圖上的四個KML圖層添加切換複選框。谷歌地圖v3:無法獲取屬性'setMap'的價值

但是,當我單擊複選框時,出現此錯誤:「Microsoft JScript運行時錯誤:無法獲取屬性'setMap'的值:object爲null或undefined」。謝謝!

下面是HTML:

<body onload="initialize()">  
     <div id="map-canvas" style="width:100%; height:90%;"></div> 
     <div id="legend"> 
      <div class="column"> 
       <input type="checkbox" id="landmarksLayer" onclick="toggleKMLLayer(this,'Notable Locations');" checked="checked" />Notable Locations (<img alt="yellow dot" src="Images/GoogleMaps/yellow-dot.png" />) 
      </div> 
     </div> 
    </body> 

這裏是JavaScript:

<%--Javascript--%> 
    <script type="text/JavaScript" src="http://maps.googleapis.com/maps/api/js?key=[I_Removed_My_Key]&sensor=false"></script> 

    <script type="text/javascript"> 

     var infowindow = new google.maps.InfoWindow({ "maxWidth": 100 }); 

     var map; 

     var landmarksLayer; 
     var publicArtLayer; 
     var blueEmergencyPhonesLayer; 
     var buildingsLayer; 

     //toggle 
     function toggleKMLLayer(chkbox, kmlLayerID) { 

      //window.alert(kmlLayerID); 

      if (chkbox.checked) { 
       //window.alert("checked"); 
       landmarksLayer.setMap(map); 
      } 
      else { 
       //window.alert("unchecked"); 
       landmarksLayer.setMap(null); 
      } 
     } 

     function initialize() { 

      var mapOptions = { 
       center: new google.maps.LatLng(33.585737, -101.884804), 
       zoom: 15, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 

      map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 

      var kmlLayerOptions = { 
       preserveViewport: true, 
       suppressInfoWindows: true 
      }; 

      var landmarksLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/landmarks.xml', kmlLayerOptions); 
      landmarksLayer.setMap(map); 

      var publicArtLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/art.xml', kmlLayerOptions); 
      publicArtLayer.setMap(map); 

      var blueEmergencyPhonesLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/emergency.xml', kmlLayerOptions); 
      blueEmergencyPhonesLayer.setMap(map); 

      var buildingsLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/bldgs.xml', kmlLayerOptions); 
      buildingsLayer.setMap(map); 


      addKmlClickHandler(landmarksLayer); 
      addKmlClickHandler(publicArtLayer); 
      addKmlClickHandler(blueEmergencyPhonesLayer); 
      addKmlClickHandler(buildingsLayer); 
     } 

     // create a new info window for the KML (outage) layer and the geo-coded house marker 
     function addKmlClickHandler(KmlLayer) { 
      google.maps.event.addListener(KmlLayer, "click", function (event) { 
       infowindow.close(); 
       infowindow.setOptions({ 
        pixelOffset: event.pixelOffset, 
        content: event.featureData.infoWindowHtml, 
        position: event.latLng 
       }); 
       infowindow.open(map); 
      }); 
     } 

    </script> 

回答

2

你正在創建全局變量圖層:

var landmarksLayer; 
    var publicArtLayer; 
    var blueEmergencyPhonesLayer; 
    var buildingsLayer; 

,但不使用他們(在你的初始化函數中):

var landmarksLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/landmarks.xml', kmlLayerOptions); 
    landmarksLayer.setMap(map); 

    var publicArtLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/art.xml', kmlLayerOptions); 
    publicArtLayer.setMap(map); 

    var blueEmergencyPhonesLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/emergency.xml', kmlLayerOptions); 
    blueEmergencyPhonesLayer.setMap(map); 

    var buildingsLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/bldgs.xml', kmlLayerOptions); 
    buildingsLayer.setMap(map); 

變量前面的「var」會創建初始化函數本地的變量的新版本,從而使全局版本未初始化。您需要初始化全局版本,以便您可以在您的HTML單擊偵聽器(在全局上下文中執行)中使用它。