简体   繁体   English

Google Maps JS:未捕获的TypeError:无法调用未定义的方法“ addDomListener”

[英]Google Maps JS: Uncaught TypeError: Cannot call method 'addDomListener' of undefined

I'm getting the " Cannot call method 'addDomListener' of undefined " error with the google.maps.event.addDomListener(window, 'load', initialize); 我收到google.maps.event.addDomListener(window, 'load', initialize);的“ 无法调用未定义的方法'addDomListener' ”的错误google.maps.event.addDomListener(window, 'load', initialize); code line, where event is 'undefined'. 代码行,其中event为“未定义”。 My code below is basically copied from an example on developer.google.com. 我的以下代码基本上是从developer.google.com上的示例复制而来的。

$(function () {
   var officeMap = $("#map-canvas");
   if (officeMap.length > 0) {
      var map;
      function initialize()  {
         var mapOptions = {
            center: new google.maps.LatLng(-34.397, 150.644), zoom: 8 
         };
         map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
      }
      google.maps.event.addDomListener(window, 'load', initialize);
   }
});

The google maps script (with API key) is loaded before this at the end of the document body (using Modernizr), looking like this: " https://maps.googleapis.com/maps/api/js?key=myApiKey&sensor=false " (I switch "myApiKey" with generate key ofc :)) google maps脚本(带有API密钥)在此之前在文档正文末尾(使用Modernizr)加载,如下所示:“ https://maps.googleapis.com/maps/api/js?key=myApiKey&sensor= false “(我用生成密钥ofc切换“ myApiKey” :)

Does the error indicate that the Google Maps script didn't load first? 该错误是否表示Google Maps脚本未首先加载? Or is my API-key invalid? 还是我的API密钥无效? Or something else? 或者是其他东西?

The google code sample is correct, but not going to run in most website given there is ajax script loading involve. 谷歌代码示例是正确的,但考虑到存在ajax脚本加载,因此不会在大多数网站上运行。 And yes you are right the google script is not loaded at the point when you call the function. 是的,您是对的,在调用函数时尚未加载google脚本。

Take a look at the source of that google script, it is not the full javascript sdk, it is just a proxy/starter file. 看看该Google脚本的来源,它不是完整的javascript SDK,它只是一个代理/启动文件。 I remember it is not like this last time when those sample code works without problem. 我记得那些样例代码没有问题时,这并不是最后一次。

 <script src="https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize"></script>
 <script>

function initialize() {
   var officeMap = $("#map-canvas");
   if (officeMap.length > 0) {
      var map;

         var mapOptions = {
            center: new google.maps.LatLng(-34.397, 150.644), zoom: 8 
         };
         map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

   }
}
 </script>

Try using this instead of your key 尝试使用它代替您的密钥

<script type="text/javascript"      src="https://maps.googleapis.com/maps/api/js?sensor=false">

My code 我的密码

var myCenter=new google.maps.LatLng(-34.397, 150.644);

        function initialize()
        {
        var mapProp = {
          center:myCenter,
          zoom:8,
          mapTypeId:google.maps.MapTypeId.ROADMAP
          };

        var map=new google.maps.Map(document.getElementById('maparea'),mapProp);

        var marker=new google.maps.Marker({
          position:myCenter,
          title:'Hi'
          });

        marker.setMap(map);

        var infowindow = new google.maps.InfoWindow({
          content:'',
          boxStyle: {
                  maxHeight:200
                }
          });

        infowindow.open(map,marker);
        }

        google.maps.event.addDomListener(window, 'load', initialize);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Maps Api 3:未捕获的TypeError:无法调用未定义的方法&#39;setCenter&#39; - Google Maps Api 3: Uncaught TypeError: Cannot call method 'setCenter' of undefined here-api未被捕获的TypeError:无法在读取未定义的属性&#39;addDomListener&#39; - here-api Uncaught TypeError: Cannot read property 'addDomListener' of undefined at Backbone.js-未捕获的TypeError:无法调用未定义的方法&#39;receivedEvent&#39; - Backbone.js - Uncaught TypeError: Cannot call method 'receivedEvent' of undefined 未捕获的TypeError:无法调用未定义的ribs.js的方法“替换” - uncaught TypeError: Cannot call method 'replace' of undefined backbone.js 未捕获的TypeError:无法在流星JS中调用未定义的方法&#39;appendChild&#39; - Uncaught TypeError: Cannot call method 'appendChild' of undefined in Meteor JS 未捕获的TypeError:无法调用undefined underscore.js的方法&#39;replace&#39; - Uncaught TypeError: Cannot call method 'replace' of undefined underscore.js 未捕获的TypeError:无法调用未定义的方法“ add”(Kinetic js) - Uncaught TypeError: Cannot call method 'add' of undefined (Kinetic js) 未捕获的TypeError:无法调用未定义的方法&#39;on&#39; - Backbone.js - Uncaught TypeError: Cannot call method 'on' of undefined - Backbone.js 未捕获的TypeError:无法调用未定义的方法“ split” - Uncaught TypeError: Cannot call method 'split' of undefined 未捕获的TypeError:无法调用未定义的方法“ Where” - Uncaught TypeError: Cannot call method 'Where' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM