Google地图无法加载.挂起authenticationService.authenticate

Sud*_*ahu 7 google-maps google-maps-api-3

我有一个Google Maps应用程序,可以在多台开发人员计算机上完美运行,但在部署到面向Internet的Web服务器时无法运行.

开发人员机器都使用访问应用程序

http://localhost
Run Code Online (Sandbox Code Playgroud)

但是,在面向Internet的Web服务器上,应用程序当然可以通过有效域访问

http://<Some Domain>/<application>
Run Code Online (Sandbox Code Playgroud)

在浏览器端调试我看到从浏览器进行的最后一次调用是

https://maps.googleapis.com/maps/api/js/AuthenticationService.Authenticate?1shttp://<the proper domain>/corpsuser/User_Page.html&callback=_xdc_._9p58yx&token=125651
Run Code Online (Sandbox Code Playgroud)

(域名掩盖)

事物挂起的地图应用程序代码似乎是javascript:

map = new google.maps.Map(document.getElementById('map'), mapOptions);
Run Code Online (Sandbox Code Playgroud)

正确定义元素'map'和对象mapOptions(毕竟,app在dev机器上工作正常)

我猜这是许可/关键问题.

我登录了用于启用Google Maps API并生成浏览器密钥的Google帐户,并且还将该域添加/关联到密钥,但这不起作用.注意到有消息表示最多可能需要5分钟才能反映出更改.所以等了一段时间,仍然没有运气.

深入挖掘,我看到一些调用,例如snapToRoads API将Server API Key作为参数.

但是,应用程序挂起的调用是设置映射的第一次调用,并且不接受API密钥.

谷歌文档说我需要在某个地方使用这个标签

<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

我应该在哪里添加它?我是否必须定义initMap函数?还是应该按原样使用?

任何帮助将不胜感激.

小智 2

这是一个示例代码,您需要在其中放置该行<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" type="text/javascript"></script>

<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -34.397, lng: 150.644},
          zoom: 8
        });
      }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"
    async defer></script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

您会注意到这些文档中的所有示例都使用该行,因为您需要它来加载 Google Maps JavaScript API。

对于回调参数,这里解释的是,当异步属性让浏览器在 Maps JavaScript API 加载时渲染网站时,当 API 准备就绪时,它将调用使用回调参数指定的函数。