我在使用 JavaScript 实现google MAP
API 时收到以下错误。
错误:
**auto.html:14 Uncaught ReferenceError: google is not defined
at auto.html:14
(anonymous) @ auto.html:14**
我提供下面的代码。
自动.html:
<!DOCTYPE html>
<html>
<head>
<title>Parent-Child Communication</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&callback=initService" async defer></script>
</head>
<body>
<input id="searchTextField" type="text" size="50">
<script type="text/javascript">
function init() {
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
}
google.maps.event.addDomListener(window, 'load', init);
</script>
</body>
</html>
我的要求是当用户输入地名会自动建议的内容时,但就我而言,它会抛出错误。
删除google.maps.event.addDomListener(window, 'load', init);
,然后将脚本包含从:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&callback=initService" async defer></script>
to(所以它调用你的init
函数(:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&callback=init" async defer></script>
工作代码片段:
<title>Parent-Child Communication</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=init" async defer></script>
<input id="searchTextField" type="text" size="50">
<script type="text/javascript">
function init() {
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
}
</script>