Uncaught ReferenceError:google未定义 [英] Uncaught ReferenceError: google is not defined
问题描述
我想使用地理位置和方向功能,但是 google没有定义
错误。代码如下:
function loadScript(){
var script = document.createElement('script') ;
script.type ='text / javascript';
script.src =https://maps.googleapis.com/maps/api/js?key=mykey&sensor=true+& callback = initialize;
document.body.appendChild(script);
}
似乎loadScript不起作用!
var mapOptions = {
zoom:13,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);
错误从此处跳出。有谁知道如何弄清楚?我需要使用键来获取地理定位服务,所以我不能使用简单的
< script src =https:// maps.googleapis.com/maps/api/js?sensor=true\"></script>
对我来说工作得很好
动态关键
<!DOCTYPE html>
< html>
< head>
< meta name =viewportcontent =initial-scale = 1.0,user-scalable = no/>
< style type =text / css>
html {height:100%; }
body
{
height:100%;
margin:0px;
padding:0px;
}
#map_canvas {height:100%;}
< / style>
< script type =text / javascript>
函数initialize(){
var latlng = new google.maps.LatLng(-34.397,150.644);
var myOptions = {
zoom:8,
center:latlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById(map_canvas),
myOptions);
}
var myKey =ENTER_YOUR_KEY_HERE;
函数loadScript(){
var script = document.createElement('script');
script.type ='text / javascript';
script.src =https://maps.googleapis.com/maps/api/js?key=+ myKey +& sensor = false& callback = initialize;
document.body.appendChild(script);
}
< / script>
< / head>
< body onload =loadScript()>
< div id =map_canvasstyle =width:100%; height:100%>
< / div>
< / body>
< / html>
静态无键
...
< / script>
< / head>
< body onload =initialize()>
...
当通过网络浏览时,我翻了一个重要的提示! p>
Google Maps JavaScript API v3
Google地图JavaScript API v3不需要
API函数正确的API密钥。不过,我们强烈建议您使用API控制台密钥加载
Maps API,以允许您监控
应用的Maps API使用情况。了解如何使用API控制台密钥。
请参阅 Google Maps API
因此,显然您不再需要开发人员密钥!我尝试了两种方法 - 静态无钥匙和动态钥匙 - 都可以。
I want to use geolocation and direction function, but there is google is not defined
error. the code is as below:
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://maps.googleapis.com/maps/api/js?key=mykey&sensor=true" + "&callback=initialize";
document.body.appendChild(script);
}
It seems that the loadScript does not work!
var mapOptions = {
zoom : 13,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
The error jumps out from here. Is anyone who know how to figure it out? I need to use key to get the geolocation service, so I cannot use simple
<script src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
I tried it on my own with this code - it worked fine for me
Dynamic with key
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100%; }
body
{
height: 100%;
margin: 0px;
padding: 0px;
}
#map_canvas { height: 100%;}
</style>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
}
var myKey = "ENTER_YOUR_KEY_HERE";
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://maps.googleapis.com/maps/api/js?key=" + myKey + "&sensor=false&callback=initialize";
document.body.appendChild(script);
}
</script>
</head>
<body onload="loadScript()">
<div id="map_canvas" style="width: 100%; height: 100%">
</div>
</body>
</html>
Static without key
...
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false">
</script>
</head>
<body onload="initialize()">
...
When surfing through the net I tumbled over an important note!
Google Maps JavaScript API v3
The Google Maps JavaScript API v3 does not require an API key to function correctly. However, we strongly encourage you to load the Maps API using an APIs Console key which allows you to monitor your application's Maps API usage. Learn how to use an APIs Console key.
See Google Maps API
So, apparently you no longer need a developer key! I tried it with both - static no key and dynamnic with key - both worked.
这篇关于Uncaught ReferenceError:google未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!