2011-05-09 141 views
0

我一直在用Google地圖測試一些東西,雖然這段代碼給了我一個帶有標記的地圖,但是我沒有得到覆蓋點擊。我實際上使用Rails,但這是我的Rails視圖正在生成的代碼 - 但我的JavaScript知識非常少。Infowindow with Google maps

<script type="text/javascript"> 
    function initialize() { 
    var latlng = new google.maps.LatLng(38.92226, -77.02515); 
    var myOptions = { 
     zoom: 15, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

    var contentString = '1X438A40'; 
    var infowindow = new google.maps.InfoWindow({ 
      content: contentString 
     }); 

    var marker = new google.maps.Marker({ 
       position: latlng, 
       map: map, 
       title:"1X438A40" 
    }); 
    } 

    google.maps.event.addListener(marker, 'click', function() { 
    infowindow.open(map,marker); 
    }); 

</script> 

回答

1

您的addListener函數不在initialize()函數的作用域中。把它放在大括號內應該可以做到。

<script type="text/javascript"> 
     function initialize() { 
     var latlng = new google.maps.LatLng(38.92226, -77.02515); 
     var myOptions = { 
      zoom: 15, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

     var contentString = '1X438A40'; 
     var infowindow = new google.maps.InfoWindow({ 
       content: contentString 
      }); 

     var marker = new google.maps.Marker({ 
        position: latlng, 
        map: map, 
        title:"1X438A40" 
     }); 

     google.maps.event.addListener(marker, 'click', function() { 
     infowindow.open(map,marker); 
     }); 
    } 

    </script> 
+0

完美,謝謝! – Slick23 2011-05-09 14:39:16