【问题标题】:Google Maps DomListener Causing Error After Going to Different PageGoogle Maps DomListener 转到不同页面后导致错误
【发布时间】: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); 

【问题讨论】:

标签: javascript maps external


【解决方案1】:

这里可能重复:

"google is not defined" when using Google Maps V3 in Firefox remotely

我认为这可能是与主题中的 Diego Pino 类似的位置问题。

【讨论】:

  • 此类信息必须作为评论而不是作为答案,因此问题可以作为重复关闭(如果问题相同)。
  • @AntoJurković 同意 Anto,实际上一开始就想这样做,但它说我需要 50 个代表发表评论。因此,如果可以的话,我把它作为帮助的答案。
  • 感谢您的所有回复 :) 我现在已经解决了这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多