2016-07-14 55 views
2

當我打開我的谷歌地圖顯示的頁面,我總是看到下面的錯誤在控制檯:谷歌地圖 - 未捕獲InvalidValueError:初始化不是一個函數

Uncaught InvalidValueError: initialise is not a function js?sensor=false&callback=initialise:94

當鼠標懸停在文件名,這顯示爲起源於https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialise

雖然Google Maps窗口和地圖顯示效果絕佳,並且具有全部功能。奇怪的是,我在Google上找不到任何關於此的點擊,它們似乎都是關於setLong和setLat的。

如果我更改API調用和JS文件之間的加載順序,錯誤消息在initialisegoogle之間變化。但在這兩種情況下,地圖仍然繼續加載正常。

爲什麼錯誤發生,以及如何正確解決它?這裏是我的谷歌,map.js文件:

function initialise() { 
    var myLatlng = new google.maps.LatLng(51.126500, 0.257595); // Add the coordinates 
    var mapOptions = { 
     zoom: 15, // The initial zoom level when your map loads (0-20) 
     disableDefaultUI: true, // Disable the map UI 
     center: myLatlng, // Centre the Map to our coordinates variable 
     mapTypeId: google.maps.MapTypeId.ROADMAP, // Set the type of Map 
     scrollwheel: false, // Disable Mouse Scroll zooming (Essential for responsive sites!) 
    // All of the below are set to true by default, so simply remove if set to true: 
    panControl:false, // Set to false to disable 
    mapTypeControl:false, // Disable Map/Satellite switch 
    scaleControl:false, // Set to false to hide scale 
    streetViewControl:false, // Set to disable to hide street view 
    overviewMapControl:false, // Set to false to remove overview control 
    rotateControl:false // Set to false to disable rotate control 
    } 
    var map = new google.maps.Map(document.getElementById('map'), mapOptions); // Render our map within the empty div 
    var image = new google.maps.MarkerImage('/wp-content/themes/bellavou/img/marker2.png', null, null, null, new google.maps.Size(70,70)); // Create a variable for our marker image.    
    var marker = new google.maps.Marker({ // Set the marker 
     position: new google.maps.LatLng(51.125887, 0.258075), // Position marker to coordinates 
     icon:image, //use our image as the marker 
     map: map, // assign the market to our map variable 
     title: 'Bella Vou at The Pantiles' // Marker ALT Text 
    }); 
} 
google.maps.event.addDomListener(window, 'load', initialise); // Execute our 'initialise' function once the page has loaded. 
+1

如果沒有發佈您的代碼,我們可能會如何幫助? – MrUpsidown

+0

不確定你需要它,但好吧我會發布它... – Lee

+2

首先,你定義了一個API調用的回調函數:https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialise '然後你添加一個監聽器'google.maps.event.addDomListener(window,'load',initialise);'基本上也是這樣。刪除該監聽器行或從API調用中除去'callback = initialise',並讓我們知道它是否更好。 – MrUpsidown

回答

13

首先,你在API調用定義一個回調:

https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialise

那麼你添加一個偵聽

google.maps.event.addDomListener(window, 'load', initialise);

基本上也是這樣。


你應該由API 上述從您的JS文件中提到addDomListener行刪除callback=initialise,它應該工作。

相關問題