2016-11-06 146 views
1

我不明白是什麼問題?我使用谷歌地圖API的這個例子:Simple MapinitMap不是函數

<body> 
    <!-- Map Section --> 
    <section id="map"></section> 

    <script src="js/jquery.min.js"></script> 
    <script src="js/main.js"></script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB5Y8CqFe-4Egzl5TlPqlFvjRGcuCfHGvY&callback=initMap" async defer></script> 

</body> 

main.js:

//-------------------------------------------------- 
// Contact Map 
//-------------------------------------------------- 
function initMap() {  
    if ($("#map").length > 0) 
    { 
     var map; 
     map = new google.maps.Map(document.getElementById('map'),{ 
      center: {lat: 44.4297538, lng: 26.0649221}, 
      zoom: 16, 
      scrollwheel: false, 
      zoomControl: false, 
      panControl: false, 
      streetViewControl: false, 
      mapTypeControl: false, 
      overviewMapControl: false, 
      clickable: false 
     }); 

    } 
} 

錯誤:在頭

message: "initMap is not a function"

name: "InvalidValueError"

回答

1

嘗試移動加載腳本

<html> 
    <head> 
    <title>Simple Map</title> 
    <meta name="viewport" content="initial-scale=1.0"> 
    <meta charset="utf-8"> 

    <script src="js/jquery.min.js"></script> 
    <script src="js/main.js"></script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB5Y8CqFe-4Egzl5TlPqlFvjRGcuCfHGvY&callback=initMap" async defer></script> 

並確保對於正確的路徑

 <script src="js/jquery.min.js"></script> 
    <script src="js/main.js"></script> 

最終嘗試使用

 <script src="./js/jquery.min.js"></script> 
    <script src="./js/main.js"></script> 
+1

感謝您的回覆!但我仍然有同樣的錯誤。 – DenysM

+0

然後檢查相對(或正確的腳本路徑) – scaisEdge

+0

當然,我檢查了它。 index.html和contact.html位於根目錄中。所以,我不需要使用./ – DenysM

1

我有同樣的問題與賢者(一個WordPress主題首發)WordPress模板工作相對路徑。

我的JS與

(function($) { 

// all the functions to create map, center markers, etc. 

} 

包裹之後,在地圖中$(document).ready

被初始化我刪除(function($) { }和剛纔添加的功能,而不$(document).ready那麼也沒關係。

此外,一定的API谷歌地圖之前加載自定義的js文件:

<script src="custom-js-google-map.js"></script> 

<script async defer 
     src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"> 
</script> 
1

有同樣的問題,我個人刪除&callback=initMap,使其工作。 其他線程表明initMap是你應該自己構建的函數。

+0

我有同樣的問題...您的解決方案工作...從腳本調用中刪除了initMap,它工作。具有諷刺意味的是,通過我相當長的一段時間,我甚至都沒有在腳本行中看到這一點,因爲我在另一個custom.js文件中有一個單獨的'initMap()' - 所以我一直在看着它,問題是。 &callback = initMap正在調用尚未加載的函數。我的custom.js文件在地圖腳本之後加載 - 故意......因此initMap尚未定義。 – rolinger