Javascript 错误 - 找不到变量:谷歌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7567602/
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
Javascript Error - Can't find variable : google
提问by SashaZd
The code I wrote runs absolutely fine on the browser. But when I connect to wifi on the iPhone I get an error in the debugger : Javascript Error - Can't find variable : google
我写的代码在浏览器上运行得非常好。但是当我在 iPhone 上连接到 wifi 时,我在调试器中收到一个错误:Javascript 错误 - 找不到变量:google
This occurs whenever I call any google maps/directions/geoLocation object.
每当我调用任何 google maps/directions/geoLocation 对象时都会发生这种情况。
The code is as follows :
代码如下:
map = new Ext.Map({
mapOptions : {
center : center,
zoom : 20,
// mapTypeId : google.maps.MapTypeId.ROADMAP,
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.DEFAULT
}
},
listeners : {
maprender : function(comp, map){
pos = new google.maps.LatLng(lats, longs, true);
var marker = new google.maps.Marker({
position: pos,
//title : 'Sencha HQ',
map: map
});
map.setMapTypeId(google.maps.MapTypeId.HYBRID);
setTimeout( function(){map.panTo (pos);} , 1000);
}
},
geo:new Ext.util.GeoLocation({
autoUpdate:true,
maximumAge: 0,
timeout:2000,
listeners:{
locationupdate: function(geo) {
pos = new google.maps.LatLng(lats, longs, true);
center = new google.maps.LatLng(geo.latitude, geo.longitude);
if (map.rendered)
map.update(center)
else
map.on('activate', map.onUpdate, map, {single: true, data: pos});
},
locationerror: function(geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) {
if(bLocationUnavailable){
alert('Your Current Location is Unavailable on this device');
}
else if (bPermissionDenied){
alert('Location capabilities have been disabled on this device.');
}
}
}
})
});
The error occurs whenever the code encounters the word google. Also for the LatLng object I get the javascript error : "....result of LatLng not a constructor"
每当代码遇到单词 google 时就会发生错误。同样对于 LatLng 对象,我收到了 javascript 错误:“....LatLng 的结果不是构造函数”
Note : the variables "lats" and "longs" have been defined n given values before this segment of code
注意:在这段代码之前,变量“lats”和“longs”已经定义了n个给定的值
采纳答案by SashaZd
So I had posted this earlier as a comment, but since this is the actual solution to the problem, I'm gonna post it once more :D
所以我早些时候发布了这个作为评论,但由于这是问题的实际解决方案,我将再次发布:D
Ok turns out that the reason I was getting all those errors is cos I was behind a proxy. Weirdly enough the proxy lets me access the google libraries and server when I'm connected using the LAN cable, but not when Im using WiFi. Hope some1 else who's stuck wid this kind of error will find this somewhat helpful :)
好吧,结果我收到所有这些错误的原因是因为我在代理后面。奇怪的是,当我使用 LAN 电缆连接时,代理允许我访问谷歌库和服务器,但当我使用 WiFi 时则不能。希望其他陷入这种错误的人会发现这有点帮助:)
回答by Victor
What worked for me: remove the "https" from the
什么对我有用:从
<script src="https://maps.google.com/maps/api/js?sensor=false"></script>
so it becomes
所以它变成
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
I don't know why Safari doesn't load secure pages on unsecured ones but it just doesn't seem to. And then throws the error. This is the only mobile browser that seems to behave like that.
我不知道为什么 Safari 不会在不安全的页面上加载安全页面,但它似乎没有。然后抛出错误。这是唯一一个看起来像这样的移动浏览器。
回答by Johannes Fahrenkrug
Could it be that you're loading the Google JavaScript libraries from the Google server? If so, when you don't have WiFi, that won't work. That's why the "google" object is not defined. You can only use Google Maps and such when you're device is online.
是不是您正在从 Google 服务器加载 Google JavaScript 库?如果是这样,当您没有 WiFi 时,这将不起作用。这就是未定义“google”对象的原因。您只能在设备在线时使用 Google 地图等。
EDIT: Nevermind, you said you ARE connected via WiFi. Odd. I guess we need to see your full source code then.
编辑:没关系,你说你是通过 WiFi 连接的。奇怪的。我想我们需要看到你的完整源代码。
回答by vLamp
My solution was to simply make sure the Google Maps js loads before the Touch js. Simply put
我的解决方案是简单地确保 Google Maps js 在 Touch js 之前加载。简单的说
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
before
前
<script id="microloader" type="text/javascript" src="touch/microloader/development.js"</script>
<script id="microloader" type="text/javascript" src="touch/microloader/development.js"</script>
回答by Carlos Galeano
Check if your config.xml have permission to navigate into Google Api Address that you write in index.html. Double check if you are working with 'https' or 'http'. I did with 'https'
检查您的 config.xml 是否有权导航到您在 index.html 中编写的 Google Api 地址。仔细检查您使用的是“https”还是“http”。我用'https'
In your index.html:
在你的 index.html 中:
`<script src="https://maps.google.com/maps/api/js?key=APIKEY" async defer type="text/javascript"></script>`
In your config.xml
在你的 config.xml
`<access origin="https://maps.google.com/*" />
<allow-intent href="https://maps.google.com/*" />
<access origin="https://maps.gstatic.com/*" />
<allow-intent href="https://maps.gstatic.com/*" />
<access origin="https://maps.googleapis.com/*" />
<allow-intent href="https://maps.googleapis.com/*" />
<access origin="https://fonts.googleapis.com/*" />
<allow-intent href="https://fonts.googleapis.com/*" />`
This worked for me!
这对我有用!