【问题标题】:How to solve the google map multiple times included Issue while using Auto-Loading feature使用自动加载功能时如何多次解决谷歌地图问题
【发布时间】:2015-08-26 07:18:08
【问题描述】:

我需要澄清加载 Google 地图 api 时出现的问题。

“您在此页面上多次包含 Google Maps API。这可能会导致意外错误。”

这是在我尝试自动加载 API 时发生的。

以下是用到的代码。让我知道哪里出了问题。

提前谢谢....!!!

index.html:

<script type="text/javascript" src="mapSrc.js"></script>
<script>
  function callSrc()
  {
     initMap();
  }
</script>

mapSrc.js:

function initMap()
{
    loadAPI(); 
}

function loadAPI()
{

    var script = document.createElement("script");
    script.src = 'https://www.google.com/jsapi?autoload={"modules"[{"name":"maps","version":"3.exp","other_params":"sensor=false"}]}&callback=loadAPI2';    
    script.type = "text/javascript";    
    document.getElementsByTagName("head")[0].appendChild(script);
}

function loadAPI2()
{       
   var script = document.createElement("script");
   script.src = "https://maps.googleapis.com/maps/api/js?sensor=false&callback=loadAPI3";
   script.type = "text/javascript";
   document.getElementsByTagName("head")[0].appendChild(script);      
}

function loadAPI3() {

    google.load('visualization', '1', {
       'packages': ['table'],
       'callback':'loaded'
    });
 }

function loaded()
{
    console.log("Google map successfully loaded...");
}

【问题讨论】:

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


    【解决方案1】:

    loadAPIloadAPI2 都加载 Google Maps Javascript API v3。你应该只做其中之一。

    您需要 jsapi,删除另一个。尽管如果您阅读了autoloading 上的文档,您应该能够在一次操作中完成所有操作。

    Note that the sensor parameter is no longer required

    【讨论】:

    • 感谢您的回复...我尝试删除其中一个,但由于 Type Error: google.load() is not a function...
    • 那你删错了。您需要 jsapi,删除另一个。尽管如果您阅读了autoloading 上的文档,您应该能够在一次操作中完成所有操作。
    • 我也尝试了其他选项,但它没用。删除 maps.googleapis.com 后,它会抛出错误,因为 TypeError: google.maps.LatLng is not a constructor。这里 LatLng 用于同一个 JS 文件中的其他函数。
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    相关资源
    最近更新 更多