【问题标题】:you have included the Google Maps API multiple times on this page. This may cause unexpected errors (wordpress)您已在此页面上多次包含 Google Maps API。这可能会导致意外错误(wordpress)
【发布时间】:2016-01-31 20:53:29
【问题描述】:

使用 google map api 时,我的控制台出现两个错误:

1-Uncaught ReferenceError: 未定义初始化

2-您已在此页面上多次包含 Google Maps API。这可能会导致意外错误 有什么帮助吗?

JS

<script type="text/javascript">

function initialize() {


 var myLatLng = {lat: 42.52501, lng: 2.938979}; 
     var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 12,
    center: myLatLng,
    scrollwheel: false,
    draggable:true,



  });
     var image='logo.png';
     var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    icon:image

  });


}

</script>

HEADER.PHP

 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key='YOUR API KEY HERE'"></script>

<script src='https://maps.googleapis.com/maps/api/js?v=3.exp'></script>

<body  onload="initialize()";></body>

【问题讨论】:

  • 密钥是敏感项目吗?如果是这样,您应该将其删除。否则——没关系。 :-) 好的,我只看了两个 标签。如果您只是打开一个新选项卡并添加这两个 https: 链接,它们都会加载相同的内容。这就是您的重复消息的来源。
  • @MarkManning 来自 Google Maps JavaScript API 的密钥,如果我删除它,将不会显示地图
  • 您收到第二个错误,因为您多次包含 API。要修复它,请删除其中一个
  • @hopo :我认为这是您需要 jQuery 的 document.ready 函数来确保在继续之前所有内容都已正确加载的时候之一。我认为这就是参考错误的来源。你也可以使用 jQuery 的 $(window).onload() 函数,而不是把它放在 BODY 标签上。
  • @MarkManning 你能给我举个例子吗?

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


【解决方案1】:

这实际上是一个例子。如果结果是答案,那么您可以将其标记为 - 但这确实是一个示例。您可以从 jQuery 网站了解如何加载 jQuery,Google 还提供了一种通过 Google 加载 jQuery 的方法。

注意:我刚刚意识到同时拥有这两个是矫枉过正的。您可能只需要 ready() 函数。

<html>
<head>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=PUT YOUR API KEY HERE"></script>

<!--script src='https://maps.googleapis.com/maps/api/js?v=3.exp'></script-->
<script src="http://localhost/test_7/jquery-1.11.3.min.js"></script>

<script type="text/javascript">

$(document).ready(function()
{
function initialize() {

 var myLatLng = {lat: 42.52501, lng: 2.938979};
     var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 12,
    center: myLatLng,
    scrollwheel: false,
    draggable:true,

  });
     var image='logo.png';
     var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    icon:image

  });

}

    initialize();
});

</script>

</head>
<body>
test
</body>
</html>

还有就是

window.onload()

而不是 $(window).onload()。对于那个很抱歉! :-)

【讨论】:

  • 不工作我得到错误:$(...).onload 不是一个函数,不显示地图
  • 然后将其取出并使用 ready() 函数。让我去看看onload jQuery函数。 BRB。
  • 是的。我刚刚进行了谷歌搜索,发现stackoverflow.com/questions/3698200/… 他们说只使用 ready() 函数。另外,我要纠正我的例子。
  • @hopo :明白了吗?还是您仍然遇到错误?
  • @hopo - 不是我的意思。你还在报错吗?如果以上没有错误,您需要选择我的答案作为正确答案 - 或者如果您想通了 - 发布正确答案。话虽这么说 - 你在使用 FireFox 吗?你有安装 Firebug 吗?它是否显示任何其他类型的错误?或者您是否在 IE 中尝试过此操作并使用开发人员的工具在程序运行时对其进行监控?
【解决方案2】:

只需替换

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>


只需替换

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places&key="></script>

<script type="text/javascript" src="//https:maps.google.com/maps/api/js?libraries=places&key="></script>

记得添加您的 google api 密钥


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多