initMap不是使用Google Maps API的功能 [英] initMap is not a function in using google maps API

查看:71
本文介绍了initMap不是使用Google Maps API的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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不是函数".谁能帮忙??

I'm new to scripting . This is my code to display the map. But it throws an uncaught error "initMap is not a function". Can anyone help ??

推荐答案

showMap 重命名为 initMap .Google希望调用名为 initMap 的函数,因为您在Maps API中加载时已传递了 callback = initMap URL参数.但是您没有使用该名称的函数,只有一个名为 showMap 的函数.

Rename showMap to initMap. Google is expecting to call a function named initMap because of the callback=initMap URL parameter you've passed them when loading in the Maps API. But you don't have a function with that name, you've only got a function called showMap.

此外,通过指定该回调参数,您无需自己显式调用initMap或showMap.因此,请从< body> 标记中删除 onload ="initMap()" .

Also, by specifying that callback parameter, you don't need to then explicitly call initMap or showMap yourself. So remove onload="initMap()" from the <body> tag.

此外,当您加载Maps API时,URL中有一个错字,而不是:

Also, when you load in the Maps API, you've got a typo in the URL, instead of:

jskey=YOUR_API_KEY

应为:

js?key=YOUR_API_KEY

最后,您会丢失我添加的所有< head> 部分,并且已将函数移至< head> 因此应在调用回调函数的时间定义它.

And finally, you're missing any <head> section, which I've added, and I've moved the function into the <head> so it should be defined by the time the callback function gets called.

所以这应该起作用:

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

这篇关于initMap不是使用Google Maps API的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆