我如何在另一个脚本中引用谷歌地图

How can I reference a google map in another script?

本文关键字:引用 谷歌地图 脚本 另一个      更新时间:2023-09-26

如何在另一个脚本中引用谷歌地图?在我的WordPress页面上,我加载javascript (a),构建我的地图,和jQuery脚本(b)。我需要找出一些方法传递一个引用到地图脚本(b)。问题是,地图是在脚本(a)的一个函数内创建的。

在脚本(a)中,我得到:

function map_maker_js( args ) {
var map = new google.maps.Map(document.getElementById( args['id'] ), myOptions);
//code for building map continues
}

在脚本(b):

jQuery.noConflict();
jQuery(document).ready(function($) { 
//say I need the map's bounds
//how can I access map, in order for this to work: 
map.getBounds();
}

我看到了这个stackoverflow解决方案,但是我不能让它工作。

将其粘贴到全局命名空间

function map_maker_js( args ) {
     window.map = new google.maps.Map(
          document.getElementById( args['id'] ), myOptions);
     //code for building map continues
}
jquery

jQuery.noConflict();
jQuery(document).ready(function($) { 
    window.map.getBounds();
}

但是确保map_maker_js先完成运行