2013-05-05 32 views
0

我正在研究一個非常簡單的Google Maps項目。谷歌地圖映射全局變量定義在一個功能,但不是在另一個?

'map'被定義爲一個全局變量。

函數'initialize'從Google Latitude獲取信息,創建地圖,設置標記和infowindow,並設置調用函數'update'的間隔。它工作正常。

函數'update'應該取消設置標記,關閉infowindow,從Latitude獲取新信息,設置一個新標記和infowindow,然後平移到新地圖中心。從'未定義'的錯誤清楚地表明,在更新函數中不能識別映射變量。由於'map'是一個全局變量,並且地圖已經被創建,所以我不應該只能引用變量嗎?

示例代碼:

<script type="text/javascript"> 
    var map = null; 
    var marker = null; 

    function initialize() { 

     map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
     marker = new google.maps.Marker({ 
       position:markerlocation, 
       map:map, 
       visible:true 
     }); 

     setInterval(function(){update()},5000); 

    } // end function initialize 

    function update() { 

     marker.setmap(null); 
     infowindow.close: 

    // do other stuff 

    } // end function update 
</script> 

回答

1

當這纔是真正的代碼「未定義」的事情是功能setmap,它應該是setMap

+0

我應該已經注意到了 - 這是它。謝謝! – 2013-05-05 19:16:41

相關問題