[英]Call function in another .js file from initialize()
在我的Google地图的初始化函数中,我需要从另一个.js文件中调用一个函数。
问题 :尝试以下代码时,触发侦听器时出现错误"Uncaught ReferenceError: getPlacesByBounds is not defined"
。 我怎么解决这个问题? 我不想将该函数移到initialize()
函数上,因为它太长了。
JS代码
var map;
function initialize() {
var center_latlng = new google.maps.LatLng(42.354183, -71.065063);
var options = {
zoom: 15,
minZoom: 11,
maxZoom: 19,
center: center_latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//instantiate map object
map = new google.maps.Map(document.getElementById("map_canvas"), options);
//attach listener to the map object.
google.maps.event.addListener(map, 'idle', function() {
getPlacesByBounds ();
});
附加信息:函数getPlacesByBounds()
在jQuery的$(function() {...});
您需要确保声明getPlacesByBounds
的代码已运行,然后再尝试使用它。 并且那个getPlacesByBounds
是可见的。
顺便说一句,您应该考虑为函数创建名称空间(例如getPlacesByBounds),以避免污染全局范围。
我的猜测是,在触发侦听器时,尚未加载jQuery中的getPlacesByBounds()。 您是否正在异步加载Google地图? 如果没有尝试,请尝试: http : //code.google.com/apis/maps/documentation/javascript/basics.html#Async
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.