2014-10-20 88 views
1

我有自己生成一個基本的谷歌地圖下面的腳本:生成谷歌地圖錯誤

$(document).ready(function(){ 

    var weatherAPP = { 

     generateMap: function(){ 

      console.log('called'); 

      var mapHolder = document.getElementById('#map'); 
      var mapOptions = { 
       center: new google.maps.LatLng(51.5072, 0.1275), 
       zoom:10, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      var map = new google.maps.Map(mapHolder, mapOptions); 

     } 

    }; 

    weatherAPP.generateMap(); 

}); 

console.log('called')被調用的罰款。

我已經包括以以下腳本:

  • 谷歌地圖API
  • jQuery的
  • 腳本

但是我收到以下錯誤:

Uncaught TypeError: Cannot read property 'offsetWidth' of null 
+0

'的getElementById( '#圖')'。你的元素其實是'id =「#map」嗎?' – Stryner 2014-10-20 10:46:59

+0

Alex 2014-10-20 10:47:48

回答

2

由於你的元素我S:<section id="map"></section>

var mapHolder = document.getElementById('#map'); 

應該

var mapHolder = document.getElementById('map'); 

您必須在您使用前綴#聲明IDS使用jQuery或CSS混淆了。

document.getElementById你必須把元素ID,因爲它是在所有

+0

非常好,謝謝我應該多加註意。 – Alex 2014-10-20 10:53:41

+0

完成了謝謝.. – Alex 2014-10-20 12:36:59

1

我沒有前綴正在發生的事情,因爲你的getElementById(「#地圖」)具有#;

如果刪除#將工作:)

http://jsfiddle.net/zbyqoaju/1/

var weatherAPP = { 

     generateMap: function() { 

      console.log('called'); 

      var mapHolder = document.getElementById('map'); 
      var mapOptions = { 
       center: new google.maps.LatLng(51.5072, 0.1275), 
       zoom: 10, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      var map = new google.maps.Map(mapHolder, mapOptions); 

     } 

    }; 

    weatherAPP.generateMap();