简体   繁体   English

Google Map API无效的值错误函数未定义?

[英]Google Map API invalid Value Error Function not defined?

I'm new to javascript and not too familiar with the google API integration. 我是javascript新手,对Google API集成不太熟悉。 I'm just trying to load a simple page with a google map for testing. 我只是想加载带有Google地图的简单页面进行测试。 I've seen different implementations and can really discern any differences which might be giving me errors in this little code block. 我已经看到了不同的实现,并且可以真正辨别出任何可能在此小代码块中给我带来错误的差异。 Can anyone tell me why I am getting getting this error? 谁能告诉我为什么我会收到此错误?

<HTML>
<HEAD>
<TITLE> MAP </TITLE>
</HEAD>

<SCRIPT src="https://maps.googleapis.com/maps/api/js?
key=**keyhere**&callback=initialize"></script>

<SCRIPT LANGUAGE ="JavaScript">

function initialize(){
var map = new google.maps.Map(document.getElementById('map'), {zoom: 3, 
center: {lat:51.500, lng:-0.201}});
}

</SCRIPT>


</BODY onload= "initialize()">
<div id="map" style="width:500px; height:400px;"></div>
<BODY>


<HTML>

You have syntax errors in your HTML, this is incorrect: HTML中有语法错误,这是不正确的:

</BODY onload= "initialize()">
<div id="map" style="width:500px; height:400px;"></div>
<BODY>

should be: 应该:

<BODY onload= "initialize()">
<div id="map" style="width:500px; height:400px;"></div>
</BODY>

You also don't need/want two calls to initialize (one in the body onload, the other in the API callback parameter), remove one. 您也不需要/想要两次调用来初始化(一个在主体onload中,另一个在API回调参数中),删除一个。

Working code: 工作代码:

 <HTML> <HEAD> <TITLE> MAP </TITLE> </HEAD> <SCRIPT src="https://maps.googleapis.com/maps/api/js?callback=initialize" async defer ></script> <SCRIPT LANGUAGE ="JavaScript"> function initialize(){ var map = new google.maps.Map(document.getElementById('map'), {zoom: 3, center: {lat:51.500, lng:-0.201}}); } </SCRIPT> <BODY> <div id="map" style="width:500px; height:400px;"></div> </BODY> <HTML> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM