【问题标题】:Google Maps API not loading. Error Message: "Uncaught ReferenceError: google is not defined"谷歌地图 API 未加载。错误消息:“未捕获的 ReferenceError:未定义 google”
【发布时间】:2019-03-31 17:33:42
【问题描述】:

在升级我的 Route Tracker 应用的过程中,我遇到了与 Google Maps API 未加载相关的问题,导致出现错误“Uncaught ReferenceError: google is not defined”。

我将 google maps API URL 包括在内,如下所示:

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

并且错误是由Javascript中的以下语句生成的:

coords = new google.maps.LatLng(myLat, myLong);

这个应用程序以前运行得很好,但不幸的是不再是因为我怀疑谷歌引入了重大变化。我已经查看了与相同错误消息对应的答案,但到目前为止无济于事。

javascript代码sn-ps如下所示:

function ShowLocation(position) 
{           
    var coords;     
    var markerOptions = {
        map: geolocationClass.map,
            position: coords 
        };

    // Fetch coordinates
    var myLat = position.coords.latitude;
    var myLong = position.coords.longitude;

    // Store previous coordinates
    coordinatesClass.lat[distanceTimeClass.counter] = myLat;
    coordinatesClass.lng[distanceTimeClass.counter] = myLong;

    // Google API-ready latitude and longitude
    coords = new google.maps.LatLng(myLat, myLong);  <--- Error here 

    .............

    .............
}

函数 ShowLocation 调用如下:

function GetLocationUpdate() 
{  
    ..............

    if (navigator.geolocation) 
    {
        geolocationClass.geoLoc = navigator.geolocation;
        geolocationClass.watchID = 
            geolocationClass.geoLoc.watchPosition(ShowLocation, 
            ErrorHandler, options);

        ...........
    }
    ............
}

【问题讨论】:

标签: javascript google-maps-api-3


【解决方案1】:

该问题是由在加载 Google Maps JavaScript API 之前调用 new google.maps.LatLng 类引起的。在documentations 中,您将在脚本标记中看到callback,如下所示:

<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap"></script> 

在脚本中:

function initMap() {
   map = new google.maps.Map....

这是为了确保在加载 JavaScript API 后,initMap 将执行。

如果 new google.maps.Map 在 JavaScript API 成功加载之前执行,它将返回错误 google is not defined。这个错误甚至在以前也很常见。 Google 引入的新变化与此无关。

希望这会有所帮助!

【讨论】:

  • 感谢您的回复,但不幸的是,我之前已经多次尝试过该路线,但均未成功。它可能仍然有效,但只有在我生成了一个新的 JavaScript API 密钥来试用我的调试版本之后,或者至少这是我从谷歌云平台支持部分收到的电子邮件中的理解。我会先这样做,然后再回来提供我的反馈。
  • 我生成了一个新的 Maps Javascript API 密钥并使用 HTTP 引用限制(即 __file_url__//android_asset/www/index.html)对其进行了限制。错误仍然是“未解决的参考错误:未定义 google”。
猜你喜欢
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
相关资源
最近更新 更多