<html>
<script src="https://maps.googleapis.com/maps/api/jskey=YOUR_API_KEY&callback=initMap">
</script>
<body onload="showMap()">
<div id="map"></div>
<script type="text/javascript">
function showMap() {
console.log('at maps');
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: { lat: -34.397, lng: 150.644 }
});
}
</script>
</body>
</html>我是个脚本新手。这是我用来显示地图的代码。但是它抛出了一个未捕获的错误"initMap不是一个函数“。有人能帮上忙吗?
发布于 2019-08-07 20:45:18
延迟回答,但您可能需要检查此answer。主要的问题是在定义map之后没有放入script。代码只是在页面上创建web元素之前查找它。
发布于 2017-09-20 19:07:42
将showMap重命名为initMap。Google希望调用一个名为initMap的函数,因为您在Maps API中加载时传递了callback=initMap URL参数。但是你没有同名的函数,你只有一个名为showMap的函数。
此外,通过指定回调参数,您不需要自己显式地调用initMap或showMap。因此,从<body>标记中删除onload="initMap()"。
此外,当您加载Maps API时,URL中有一个拼写错误,而不是:
jskey=YOUR_API_KEY它应该是:
js?key=YOUR_API_KEY最后,您遗漏了我添加的任何<head>部分,我将该函数移到了<head>中,以便在调用回调函数时对其进行定义。
因此,这应该是可行的:
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
<script type="text/javascript">
function initMap() {
console.log('at maps');
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: { lat: -34.397, lng: 150.644 }
});
}
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>发布于 2019-04-19 00:59:43
移动google.map应用编程接口调用
<script src="https://maps.googleapis.com/maps/api/js?key=API_CODE&callback=initMap" async defer></script>在定义了function initMap()之后,我修复了这个问题。我猜Javasrcipt是按顺序运行的,所以当它在google.map回调中定义时,它看不到initMap()函数。
https://stackoverflow.com/questions/46319676
复制相似问题