Javascript Google Maps Uncaught TypeError:无法读取未定义的属性“LatLng”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13745693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps Uncaught TypeError: Cannot read property 'LatLng' of undefined
提问by mike628
I keep getting this error "Uncaught TypeError: Cannot read property 'LatLng' of undefined"
when trying to create a map
In the Head:
尝试
在头部创建地图时,我不断收到此错误“未捕获的类型错误:无法读取未定义的属性 'LatLng'” :
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js"/>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true"></script>
.............
………………
function init(){
var latlng = new google.maps.LatLng(-43.552965, 172.47315);
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), myOptions);
}
.............
………………
<body>
<div id="map" style="width:600px;height: 600px;">
</div>
<ul class="navigation">
</ul>
</body>
采纳答案by geocodezip
Looks like the problem is this is missing the closing tag for <script>for the include of jquery.js:
看起来问题是缺少<script>jquery.js 包含的结束标记:
<script
type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js"/>
<script
type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true">
</script>
<script>tags need to be closed with </script>, it should be:
<script>标签需要用 关闭</script>,它应该是:
<script
type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js">
</script>
<script
type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true">
</script>
For more information see: Why don't self-closing script tags work?
有关更多信息,请参阅:为什么自闭合脚本标签不起作用?
回答by RedBirdISU
Both the answers above will solve this problem if used together. Property LatLng is undefined because googleobject is not yet available.
如果一起使用,上面的两个答案都可以解决这个问题。属性 LatLng 未定义,因为google对象尚不可用。
You always close your <script>tag period.
你总是关闭你的<script>标记期。
googleobject wont be available till DOM is loaded. So in you javascript you have to use the google map's addDomListener(). Kara's solution is right but it wont work in your case since function name is init and addDomListener has to wait for "load". You would need:
google在加载 DOM 之前,对象将不可用。所以在你的 javascript 中你必须使用谷歌地图的addDomListener(). Kara 的解决方案是正确的,但它不适用于您的情况,因为函数名称是 init 并且 addDomListener 必须等待“加载”。你需要:
google.maps.event.addDomListener(window, 'load', init);
Another very easy solution for this is to add the callback to the script
另一个非常简单的解决方案是将回调添加到脚本中
src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true&callback=init
callback will wait for the script to load and then trigger your initfunction to initialize and draw your map.
回调将等待脚本加载,然后触发您的init函数来初始化和绘制您的地图。
I put the solution on CodePen here http://codepen.io/redbirdisu/pen/BHivq
我把解决方案放在 CodePen 上http://codepen.io/redbirdisu/pen/BHivq
回答by ?zgür Kara
When do you run your init function? if init function runs before google.js loaded, you can get this error.
你什么时候运行你的 init 函数?如果 init 函数在 google.js 加载之前运行,你会得到这个错误。
try call init function like this.
尝试像这样调用 init 函数。
function init(){
//...
}
google.maps.event.addDomListener(window, 'init', Initialize);
回答by maxxi
I also had the same issue, all syntax was correct. Tried to fix by moving script files up/down of the HTML file but none of them was successful.
我也有同样的问题,所有语法都是正确的。试图通过将脚本文件向上/向下移动 HTML 文件来修复,但没有一个成功。
What I did was, removed the &callback=initfrom the URL and call that method inside the $(document).ready(function () {}). Now it works perfectly.
我所做的是,&callback=init从 URL 中删除并在$(document).ready(function () {}). 现在它完美地工作。

