Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌地图API异步加载问题。导致事件运行多次_Javascript_Jquery_Ajax_Google Maps_Asynchronous - Fatal编程技术网

Javascript 谷歌地图API异步加载问题。导致事件运行多次

Javascript 谷歌地图API异步加载问题。导致事件运行多次,javascript,jquery,ajax,google-maps,asynchronous,Javascript,Jquery,Ajax,Google Maps,Asynchronous,我已经试着解决这个问题好几个星期了,最近有点忽略了,但它真的开始让我恼火了。如果有人能告诉我我的代码出了什么问题,我将不胜感激 一切都正常,除了它使我的代码运行两次,当我动态加载ajax内容时,它使我的代码运行更多次 我相当肯定这是因为异步加载GoogleMaps时的回调 if (window.google && google.maps) { initialize(); } else { if (!$('script[src="ht

我已经试着解决这个问题好几个星期了,最近有点忽略了,但它真的开始让我恼火了。如果有人能告诉我我的代码出了什么问题,我将不胜感激

一切都正常,除了它使我的代码运行两次,当我动态加载ajax内容时,它使我的代码运行更多次

我相当肯定这是因为异步加载GoogleMaps时的回调

    if (window.google && google.maps) {
        initialize();
    } else {
        if (!$('script[src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=xxxxxxxxxxxxxxxxx&signed_in=true&language=en&callback=initialize"]').length > 0) {
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp' + '&key=xxxxxxxxxxxxxxxxx&signed_in=true&language=en&callback=initialize';
            document.body.appendChild(script);
        }
    }
这就是我加载地图的方式。如果你刷新站点,它就会跳转到Else。如果我在使用Ajax动态加载的站点周围移动,它将进入If并运行initialize()

现在。。。一个简单的console.log('test');不管发生什么事,都要开火两次。如果我离开现场,然后再回去,它会开火三次,如果我再回去一次。。。您猜对了,四次测试是在控制台中编写的

现在,这就是我的问题所在吗?或者它看起来正常吗?我应该开始看看用ajax加载我的内容的脚本

这是我的初始化函数

function initialize() {

    var cell;

    $('.integrator-country').text(localStorage.country);

    $("#select-country").children("option[value='" + localStorage.country + "']").prop('selected', true);
    geocoder = new google.maps.Geocoder();

    var latlng = new google.maps.LatLng(localStorage.latitude, localStorage.longitude);
    var mapOptions = {
        zoom: 7,
        center: latlng
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    google.maps.event.addDomListener(window, "resize", function () {
        var center = map.getCenter();
        google.maps.event.trigger(map, "resize");
        map.setCenter(center);
    });

    google.maps.event.addDomListener(window, 'load', function () {
        map.setCenter(latlng);
    });

    getIntegrators(localStorage.country, cell);
}

您是否需要
if(!$('script…)
检查并检查
window.google.maps
?我怀疑它多次加载地图脚本是的,我需要它,否则地图将不会加载我的设置。我将使用initialize函数更新我的问题。如果您获得多个console.log,请检查脚本在源代码中显示的频率。可能您的if语句不正确工作正常,并且你不断多次追加脚本,因此每次我切换到页面时都会添加多个回调。不确定这是否说明了什么…除此之外,我真的看不到任何变化…触发多次的不仅仅是google map事件。我有一个拖放事件,它会向页面添加文本div,它也会成倍增加,并且将越来越多的相同文本添加到div中,我越是往返于页面。也许这些信息是相关的。