"您已在此页面上多次添加Google Maps API"错误

时间:2017-07-07 11:30:20

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

我的html页面中有以下内容:

    <script type="text/javascript"
            src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false">
    </script>
    <script 
        async defer
        src="https://maps.googleapis.com/maps/api/js?key=hUnDAdjYG_Wz7u2qL6unHqfBOmvaZ0H1Mg&callback=initMap">
    </script>

第一个链接适用于Google的API Geometry Library,第二个链接初始化并绘制地图。

我收到错误&#34;您已在此页面上多次添加Google Maps API。这可能会导致意外错误。&#34;

我知道只需调用一个脚本并更改参数即可解决此问题,请参阅Fixing "You have included the Google Maps API multiple times on this page. This may cause unexpected errors."我不知道如何为我的问题复制答案。

2 个答案:

答案 0 :(得分:6)

您可以添加一个链接:

<script type="text/javascript"
        src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false&key=AIzaSyBSsKUzYG_Wz7u2qL6unHqfBOmvaZ0H1Mg&callback=initMap">
</script>

基本上合并两个链接中的url参数。

答案 1 :(得分:1)

遇到了同样困扰我很长时间的问题。我的情况是结合 initMap 加载地图和 Google Places API 以列出自动完成输入搜索。

这是我的解决方案。

  1. 我在 initMap() 中放置的自动完成功能
<块引用>
function activatePlacesSearch() {

var input = document.getElementById('inputValue');
    var autocomplete = new google.maps.places.Autocomplete(input);
}
activatePlacesSearch();
  1. 在 index.html 中:
src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=places&callback=initMap"></script>

所以最好的解决办法是:

a) 将两个脚本合二为一

b) 如果可能,将 sensro 功能放在 initMap 中。