转到其他页面后,Google Maps DomListener导致错误 [英] Google Maps DomListener Causing Error After Going to Different Page

查看:159
本文介绍了转到其他页面后,Google Maps DomListener导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,即外部Javascript文件中google maps函数底部的dom侦听器导致未定义google"控制台错误.当我转到网站上的另一个HTML页面(该页面也访问外部文件中的脚本)时,就会发生这种情况.我相信这是因为DomListener在函数外部,但是如果dom侦听器在函数内部,则该函数将不起作用.有人可以帮我把该DomListener放在哪里,以防止发生此错误吗?我的代码在下面.

I am having an issue where the dom listener at the bottom of my google maps function in an external Javascript file is causing a "google is not defined" console error. This happens when I go to a different HTML page on my website that also accesses script in the external file. I believe it is because the DomListener is outside the function however the function does not work if the dom listener is inside the function. Can someone please help me with where to put this DomListener to stop this error occurring? My code is below.

非常感谢, 安东尼

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不是定义的"在Firefox中远程使用Google Maps V3时

我认为这可能是类似于迭戈·皮诺在线程中的布局问题.

I think its might be a placement problem similar to Diego Pino in the thread.

这篇关于转到其他页面后,Google Maps DomListener导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆