Javascript Google Maps v3,错误消息“Google 未定义”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8193546/
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 v3, error message "Google is undefined"
提问by user499846
I am getting errors on my site just trying to implement the basic "Hello world" application:
我在我的网站上遇到错误只是试图实现基本的“Hello world”应用程序:
<div id="map"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
$(document).ready(function () {
map = new google.maps.Map(document.getElementById("map"), myOptions);
});
The error I get is:
我得到的错误是:
'Uncaught ReferenceError: google is not defined'
'未捕获的 ReferenceError:未定义谷歌'
How do I fix this problem?
我该如何解决这个问题?
回答by tpeczek
Your source is wrong, I also suggest specifing the exact version you want to load:
您的来源是错误的,我还建议您指定要加载的确切版本:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&sensor=true"></script>
That should help.
那应该有帮助。
回答by defau1t
Lots of things from your code:
你的代码中有很多东西:
You have given
document.ready
but there is nojQuery
linkYour
document.ready
has to be inside<script>
tags</script>
You should also have given the
height
andwidth
to thediv
in which you are holding the map, as such it is just an empty tag.You have defined myOptions for Map, and there is nothing to interpret for map as what are the options to be taken into consideration while loading the map**
你给了
document.ready
但是没有jQuery
链接你
document.ready
必须在<script>
标签里面</script>
你也应该给的
height
,并width
给div
你拿着地图,因此在它只是一个空的标签。您已经为 Map 定义了 myOptions,对于 map 没有什么可以解释为加载地图时要考虑的选项**
Here is a Complete example:
这是一个完整的例子:
<html>
<body>
<div id="map" style="width:500px;height:500px;"></div>
<script type="text/javascript" src="Please put your jquery here"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var myLatlng = new google.maps.LatLng(40.65, -73.95);
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
$("document").ready(function () {
map = new google.maps.Map(document.getElementById("map"), myOptions);
});
</script>
</body>
</html>
回答by Memonic
Sometimes this happens in the browser Google Chrome if your main site is in a https platform and the map is in a http. For showing the map you have a shield in URL box, just click the shield and it should show it.
如果您的主站点在 https 平台中并且地图在 http 中,有时会在浏览器 Google Chrome 中发生这种情况。要显示地图,您在 URL 框中有一个盾牌,只需单击盾牌,它就会显示出来。