JavaScript 错误 - 找不到变量:谷歌

发布于 2024-12-06 17:21:04 字数 2010 浏览 0 评论 0 原文

我编写的代码在浏览器上运行得非常好。但是当我连接到 iPhone 上的 wifi 时,我在调试器中收到错误: Javascript 错误 - 找不到变量:google

每当我调用任何 google 地图/方向/geoLocation 对象时,都会发生这种情况。


代码如下:

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.');
                 }      
             }
         }
     })
});

每当代码遇到单词 google 时就会发生错误。另外,对于 LatLng 对象,我收到 javascript 错误:“...LatLng 的结果不是构造函数”

注意:变量“lats”和“longs”已在这段代码之前定义了 n 个给定值

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

This occurs whenever I call any google maps/directions/geoLocation object.


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"

Note : the variables "lats" and "longs" have been defined n given values before this segment of code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不再让梦枯萎 2024-12-13 17:21:04

对我有用的方法是:从 中删除“https”,

<script src="https://maps.google.com/maps/api/js?sensor=false"></script>

这样

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

我不知道为什么 Safari 不会在不安全的页面上加载安全页面,但它似乎没有。然后抛出错误。这是唯一一个具有这种行为的移动浏览器。

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.

三人与歌 2024-12-13 17:21:04

所以我之前已经将其作为评论发布了,但由于这是问题的实际解决方案,我将再次发布它:D

好吧,事实证明我收到所有这些错误的原因是因为我在代理后面。奇怪的是,当我使用 LAN 电缆连接时,代理允许我访问 google 库和服务器,但当我使用 WiFi 时则不允许。希望遇到这种错误的其他人会发现这有点帮助:)

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

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 :)

铁轨上的流浪者 2024-12-13 17:21:04

您是否正在从 Google 服务器加载 Google JavaScript 库?如果是这样,当你没有 WiFi 时,这是行不通的。这就是为什么“google”对象没有定义。您只能在设备在线时使用 Google 地图等。

编辑:没关系,你说过你是通过 WiFi 连接的。奇怪的。我想我们需要查看您的完整源代码。

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.

EDIT: Nevermind, you said you ARE connected via WiFi. Odd. I guess we need to see your full source code then.

且行且努力 2024-12-13 17:21:04

我的解决方案是简单地确保 Google Maps js 在 Touch js 之前加载。只需将

放在前面

My solution was to simply make sure the Google Maps js loads before the Touch js. Simply put

<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>

泪眸﹌ 2024-12-13 17:21:04

检查您的 config.xml 是否有权导航到您在 index.html 中写入的 Google Api 地址。
仔细检查您是否使用“https”或“http”。我在你的index.html中使用'https'

`<script src="https://maps.google.com/maps/api/js?key=APIKEY" async defer type="text/javascript"></script>`

在你的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/*" />`

这对我有用!

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'

In your index.html:

`<script src="https://maps.google.com/maps/api/js?key=APIKEY" async defer type="text/javascript"></script>`

In your 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文