您在此页面上多次添加了谷歌地图API.这可能会导致意外错误(wordpress)



使用谷歌地图 API 时,我在控制台中收到两个错误:

1-未捕获的引用错误:未定义初始化

2-您在此页面上多次包含谷歌地图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>

页眉。.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>

这实际上是一个例子。如果事实证明它是答案,那么您可以将其标记为这样 - 但这确实是一个例子。你可以从jQuery网站找到如何加载jQuery,谷歌也提供了一种通过谷歌加载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()。 对不起!:-)

只需替换

<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>

记得添加您的谷歌API密钥


相关内容

最新更新