javascript 未捕获的参考错误:未定义 google
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26338704/
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
Uncaught Reference Error: google is not defined
提问by Santi
I saw a dozen of questions with the same title & the problem. After going through all those problems & making changes in the code, I wasn't able to resolve this issue.
我看到了十几个具有相同标题和问题的问题。在经历了所有这些问题并更改代码后,我无法解决这个问题。
My Google map works fine. But in the console, this error is shown. Can anyone please help me figure out this problem.
我的谷歌地图工作正常。但是在控制台中,会显示此错误。任何人都可以帮我解决这个问题。
I specified JavaScript code inside the head tag
我在 head 标签内指定了 JavaScript 代码
<script>
function loadScript()
{
var myKey = "__API-Key__";
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?key="+myKey+"&sensor=false&callback=initialize";
document.body.appendChild(script);
}
window.onload = loadScript;
</script>
<script>
function initialize()
{
var laa=-34.397;
var lonn= 150.644;
var mapOptions =
{
zoom: 7,
center: new google.maps.LatLng(laa, lonn),
mapTypeId: google.maps.MapTypeId.ROADMAP,
maxZoom: 8,
minZoom:2
};
var map = new google.maps.Map(document.getElementById('location-canvas'),
mapOptions);
var marker = new google.maps.Marker({
map: map,
draggable: false,
position: new google.maps.LatLng(laa, lonn)
});
function bind(eventName)
{
google.maps.event.addListener(map, eventName, function ()
{
common();
});
}
bind('zoom_changed');
bind('center_changed');
bind('tilesloaded');
bind('idle');
function common()
{
var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var getcentre=bounds.getCenter();
var ne = map.getBounds().getNorthEast();
var sw = map.getBounds().getSouthWest();
var zoom=map.getZoom();
var centre_lat=getcentre.lat();
var centre_long=getcentre.lng();
var myLatlng=new google.maps.LatLng(centre_lat,centre_long);
var mapProp =
{
center: new google.maps.LatLng(centre_lat,centre_long),
zoom:zoom,
maxZoom: 8,
minZoom:2,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
}
}
google.maps.event.addDomListener(window, 'resize', initialize);
google.maps.event.addDomListener(window, 'load', initialize);
</script>
Inside Body tag:
身体内部标签:
<div id='location-canvas' style='width:100%;height:500px;'>
</div>
回答by Santi
You Don't need to include bellow two lines because you already define a callback function with http://maps.googleapis.com/maps/api/js?key="+myKey+"&sensor=false&callback=initialize
您不需要包含以下两行,因为您已经定义了一个回调函数 http://maps.googleapis.com/maps/api/js?key="+myKey+"&sensor=false&callback=initialize
So either use
所以要么使用
<script src="http://maps.googleapis.com/maps/api/js?key="+__API-Key__+"&sensor=false" type="text/javascript"></script>
<script src="http://maps.googleapis.com/maps/api/js?key="+__API-Key__+"&sensor=false" type="text/javascript"></script>
Without function call, put it directly on document
不用函数调用,直接放在文档上
or Remove
或删除
google.maps.event.addDomListener(window, 'resize', initialize);
google.maps.event.addDomListener(window, 'load', initialize);
e.g:
例如:
<script src="http://maps.googleapis.com/maps/api/js?key="+__API-Key__+"&sensor=false"></script>
<script>
function initialize()
{
var laa=-34.397;
var lonn= 150.644;
var mapOptions =
{
zoom: 7,
center: new google.maps.LatLng(laa, lonn),
mapTypeId: google.maps.MapTypeId.ROADMAP,
maxZoom: 8,
minZoom:2
};
var map = new google.maps.Map(document.getElementById('location-canvas'),
mapOptions);
var marker = new google.maps.Marker({
map: map,
draggable: false,
position: new google.maps.LatLng(laa, lonn)
});
function bind(eventName)
{
google.maps.event.addListener(map, eventName, function ()
{
common();
});
}
bind('zoom_changed');
bind('center_changed');
bind('tilesloaded');
bind('idle');
function common()
{
var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var getcentre=bounds.getCenter();
var ne = map.getBounds().getNorthEast();
var sw = map.getBounds().getSouthWest();
var zoom=map.getZoom();
var centre_lat=getcentre.lat();
var centre_long=getcentre.lng();
var myLatlng=new google.maps.LatLng(centre_lat,centre_long);
var mapProp =
{
center: new google.maps.LatLng(centre_lat,centre_long),
zoom:zoom,
maxZoom: 8,
minZoom:2,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
}
}
google.maps.event.addDomListener(window, 'resize', initialize);
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id='location-canvas' style='width:100%;height:500px;'>
</div>
This should works fine.
这应该可以正常工作。