【问题标题】:ReferenceError: google is not definedReferenceError:谷歌未定义
【发布时间】:2012-08-28 06:23:21
【问题描述】:

我在我的网站中使用谷歌地图的 api 来显示几个位置。谷歌地图在我的本地解决方案中工作得很好,但在我的网站上却不行。我改变了这个来源

<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.5&sensor=false"> </script>

到这个。我再次改变了这个

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

有了这个……

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

它只说:ReferenceError: google is not defined

有没有人熟悉这个问题?

【问题讨论】:

    标签: google-maps


    【解决方案1】:

    由于我的网站使用 https 进行连接,我无法使用http://maps.google.com/maps/api/js?sensor=false。当我调试整个页面时,此链接显示:警告:页面 index.html 运行不安全的内容。所以我在谷歌上再次搜索并遇到了这个question。所以基本上导致问题的原因是没有在源代码部分使用 https 链接,所以正确的链接将是(对我而言)

    https://maps.google.com/maps/api/js?sensor=false
    

    现在一切正常!

    【讨论】:

    • @ParthChavda,尝试使用 //maps.google.com/maps/api/js?sensor=false
    【解决方案2】:

    这是一个旧网址。如果您查看examples from the documentation,几乎所有这些都使用:

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

    【讨论】:

    【解决方案3】:

    对我来说,使用新的 Google Maps API,这个解决方案很有效: 只需从 goolge 提供的脚本中省略“async defer”即可:

    <script async defer src="https://maps.googleapis.com/maps/api/js?key={your_key}"></script>
    

    <script src="https://maps.googleapis.com/maps/api/js?key={your_key}"></script>
    

    【讨论】:

    • 您的答案是正确的,但应该有一些其他的解决方法。喜欢而不是使用document.ready,我们可以检查google 变量以检查是否准备就绪。
    【解决方案4】:

    我在 Rails 应用程序中遇到了这个问题。

    对 googlemap 的请求来自 http 和 https 页面

    它解决了:

    = javascript_include_tag "//maps.google.com/maps/api/js?sensor=false"
    

    如果协议未定义 rails 自动插入

    【讨论】:

      猜你喜欢
      • 2019-06-13
      • 1970-01-01
      • 2014-07-27
      • 2016-07-07
      • 2020-09-20
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      相关资源
      最近更新 更多