为什么我得到了window.initMarkers不是一个功能'

时间:2015-09-01 11:34:59

标签: javascript asynchronous google-maps-api-3 jsfiddle

我尝试使用JSFiddle异步加载Google地图,但我不断收到window.initMarkers is not a function错误。

有些人遇到了同样的问题,他们通过将其移到$(document).ready()之外来解决问题。但我需要使用jQuery。

所以有谁知道我怎么能做到这一点? My fiddle here

var map;

var GoogleMaps = {
    loadGoogleMapScript: function (callback) {
      (...)
      script.src = 'https://maps.googl...sensor=false&callback=' + callback;        
    },

    setMarkers: function (stores) {
       (...)
    }
};

function initMarkers(){
    var stores = [
        ['Store 1', 59.9136813, 10.7421917],
        ['Store 2', 59.9136287, 10.7419084],
    ];
    GoogleMaps.setMarkers(stores);    
}


GoogleMaps.loadGoogleMapScript('initMarkers');

1 个答案:

答案 0 :(得分:2)

这是一个范围问题

变化

function initMarkers(){ ...

window.initMarkers = function() { ...

这使得initMarkers成为一个全局函数,可以通过加载的google脚本中的“回调”来运行。函数声明如下

function initMarkers() { ...

inside document.ready不是全局范围的

相关问题