Javascript &引用;“谷歌未定义”;集成Maps API时
我试图在我的Web应用程序中集成Google地图,但我的代码给出了错误“Google未定义” 有人能帮我弄清楚吗?提前谢谢Javascript &引用;“谷歌未定义”;集成Maps API时,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我试图在我的Web应用程序中集成Google地图,但我的代码给出了错误“Google未定义” 有人能帮我弄清楚吗?提前谢谢 </head><script src="http://maps.google.com/maps/api/js?key=AIzaSyAdXcTNKKaje1AtGQq32SwOMJ5Um-EE5GU " async="" defer="defer" type="text/javascript"></script> 您正在延迟Google库
</head><script src="http://maps.google.com/maps/api/js?key=AIzaSyAdXcTNKKaje1AtGQq32SwOMJ5Um-EE5GU " async="" defer="defer" type="text/javascript"></script>
您正在延迟Google库include,因此当运行
LoadGoogle()
时,库不一定已加载
有两种方法:
- 对于测试,请从include中删除async/defer
- 对于生产,请确保在加载所有其他内容后运行自定义脚本。实现这一点的方法是使用google提供的回调,因此脚本在加载时调用LoadGoogle函数
然后在脚本中,不要调用LoadGoogle()
使用谷歌地图
回调功能。加载google maps脚本后,它将调用该方法。有关回调参数的解释,请参阅编辑,有关回复,请参阅教程Hanks。现在我没有得到任何错误,但地图也没有显示。
<body> <div id="map" style="width: 400px; height: 300px;"></div><script type="text/javascript">
function LoadGoogle()
{ if(typeof google != 'undefined' && google && google.load)
{
var address = 'UK';
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.TERRAIN,
zoom: 6
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
map.setCenter(results[0].geometry.location);
}
});
}
else
{
setTimeout(LoadGoogle, 30);
}
}
LoadGoogle(); </script></body>
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.TERRAIN,
zoom: 6
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
},
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
map.setCenter(results[0].geometry.location);
}
});