javascript - Uncaught ReferenceError : google is not defined in google maps

标签 javascript google-maps google-maps-api-3

在 java 脚本中出现错误,一切正常,那么为什么会出现此错误, 我将无法运行写在下面的代码,这是使用起来很重要的谷歌函数,所以我该如何删除或者是否有任何其他方法来解决错误

//Error is given on this line
google.maps.event.addDomListener(window, 'load', initGoogleMap);

//because of error this code not works
        google.maps.event.addListener(marker, 'click', function() {
            if(!marker.open){
                infoWindow.open(map,marker);
                marker.open = true;
            }
            else{
                infoWindow.close();
                marker.open = false;
            }
            google.maps.event.addListener(map, 'click', function() {
                infoWindow.close();
                marker.open = false;
            });
        });

最佳答案

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

以上代码用于调用函数initGoogleMap在窗口加载时。所以会报错的原因如下:

  1. 函数initGoogleMap你还没有定义。
  2. 这是在谷歌地图库加载之前调用。

要解决此问题,请遵循以下声明:

  • 删除或评论此行。
  • 添加代码 onload = initGoogleMap()在正文标签中。之后你的 body 标签将是这样的:

<body onload = initGoogleMap()>

希望对您有所帮助。如果您有任何疑问,请问我。

关于javascript - Uncaught ReferenceError : google is not defined in google maps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47279403/

相关文章:

android - App转账和api key 问题

javascript - 谷歌地图标记停止

javascript - 使用 Javascript v3 API 向 Google Map 添加多个点

JavaScript 按钮 SRC

javascript - 动态添加的view没有样式功能React Native

javascript - Vuejs 2 : debounce not working on a watch option

javascript - Prop `loadingElement` 在 `withScriptjs(withGoogleMap(Component)) 中被标记为必需

javascript - JSON 中可以引用吗?

css - 旋转谷歌地图容器而不是 map 本身

javascript - 谷歌地图 API v3 : Control the direction of a street view and use address instead of lat/lon