【发布时间】:2014-06-07 17:52:36
【问题描述】:
我遇到了一个问题,即外部 Javascript 文件中我的 google maps 函数底部的 dom 侦听器导致“google is not defined”控制台错误。当我转到我网站上的另一个 HTML 页面时会发生这种情况,该页面也访问外部文件中的脚本。我相信这是因为 DomListener 在函数之外,但是如果 dom 侦听器在函数内部,则该函数不起作用。有人可以帮我把这个 DomListener 放在哪里来阻止这个错误的发生吗?我的代码如下。
非常感谢, 安东尼
function validateEmail() {
var email = form.emailaddress.value;
if (form.firstname.value == "") {
document.getElementById("emailInvalid").style.visibility = "visible";
return false;
} return true;
}
function initialize() {
var myLatlng1 = new google.maps.LatLng(-25.363882,150.044922);
var myLatlng2 = new google.maps.LatLng(-25.363882,152.044922);
var mapOptions = {
zoom: 6,
center: myLatlng1
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var contentString1 = 'Mott Park'
var contentString2 = 'Kilpa Park'
var infowindow = new google.maps.InfoWindow({
});
var marker1 = new google.maps.Marker({
position: myLatlng1,
map: map,
title: 'Park'
});
var marker2 = new google.maps.Marker({
position: myLatlng2,
map: map,
title: 'Water'
});
google.maps.event.addListener(marker1, 'click', function initialize() {
infowindow.close();//hide the infowindow
infowindow.setContent(contentString1);//update the content for this marker
infowindow.open(map, marker1);
});
google.maps.event.addListener(marker2, 'click', function initialize() {
infowindow.close();//hide the infowindow
infowindow.setContent(contentString2);//update the content for this marker
infowindow.open(map, marker2);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
【问题讨论】:
-
"google is not defined" when using Google Maps V3 in Firefox remotely 的可能副本。作为用户@Manpreet Didwal 的建议添加
-
如果重复项不正确,您能否提供信息如何链接/包含 Google API 以及位置。
标签: javascript maps external