Javascript 未捕获的 ReferenceError:未定义 google
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12500654/
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 ReferenceError: google is not defined
提问by user1683941
I want to use geolocation and direction function, but there is google is not defined
error. the code is as below:
我想使用地理定位和方向功能,但有google is not defined
错误。代码如下:
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!
似乎loadScript 不起作用!
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
错误从这里跳出来。有谁知道如何弄清楚吗?我需要使用密钥来获取地理定位服务,所以我不能使用 simple
<script src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
采纳答案by Farid Abbas
If you people are getting Error in console then here is the simple solution that I applied.
如果您在控制台中遇到错误,那么这是我应用的简单解决方案。
include the script files in given sequence..
按给定顺序包含脚本文件..
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
You must first include 'maps.googleapis.com/maps/api/js?sensor=false' first then go for jquery library and remove it from below both (it will work.) I hope it will definitely work.
您必须首先包含 'maps.googleapis.com/maps/api/js?sensor=false' 然后首先使用 jquery 库并将其从两者下方删除(它会起作用)。我希望它肯定会起作用。
回答by Pilgerstorfer Franz
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.
谷歌地图 JavaScript API v3
Google Maps JavaScript API v3 不需要 API 密钥即可正常运行。但是,我们强烈建议您使用 API 控制台密钥加载 Maps API,这样您就可以监控应用程序的 Maps API 使用情况。了解如何使用 API 控制台密钥。
See Google Maps API
So, apparently you no longer need a developer key! I tried it with both - static no keyand dynamnic with key- both worked.
因此,显然您不再需要开发人员密钥!我尝试了两者 -静态无键和动态有键-都有效。
回答by zahid ullah
Google is not defined mean that your google map libary is not loaded which mean you are on https or http and you are requesting via http ot https so change it to https or http .... mean if you are on http then
谷歌未定义意味着你的谷歌地图库没有加载,这意味着你在 https 或 http 上,你通过 http ot https 请求所以将其更改为 https 或 http .... 意味着如果你在 http 上
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
mean if you are on https then
意思是如果你在 https 上,那么
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>