“谷歌未定义”在 Firefox 中远程使用 Google 地图 V3 时

发布于 2024-11-19 14:30:51 字数 1216 浏览 7 评论 0 原文

这是我的难题: 我有一个使用 Google Maps V3 和 jQuery 的页面。它在 FF5、Chrome 和 Safari 本地运行良好。

上传到网站后,我在尝试使用 google 对象的第一行收到“google 未定义”错误。

var defaultLocation = new google.maps.LatLng(lat, lng);

仅发生在 FF仅发生在远程< /strong> (即,如果我将文件加载到本地 FF 中,效果很好)。不管怎样,Chrome 和 Safari 似乎都运行良好,我的 Android 和 iPod 浏览器也是如此。

这是我到目前为止所尝试的:

  1. 移动
  2. $(function() {...}); 的所有内容移至名为 initialize() 的函数,并添加
  3. 使用脚本和 css 文件顺序播放
  4. 粘贴了 URL http://maps.google.com/maps/api/js?sensor=false 转换为 FF 地址框并验证我得到了合法的脚本

,但由于这仅发生在远程计算机上的 FF 中并且在其他情况下运行良好,我认为它与我的代码没有任何关系。也许FF5中的加载顺序是搞砸的。也许它对网络资源的优先级划分与其他浏览器不同。我现在真的不知道该怎么办。

如有任何帮助,我们将不胜感激。
盖伊

更新:
只是想添加以下事实:在 Mac 上尝试了前一个之后,我在 Windows 中尝试了 FF5,并且复制了完全相同的行为。
为了更好地衡量,我也尝试了 Pale Moon - 相同的结果。 Chrome 14、Opera 11.50 甚至该死的 IE9(未包含在测试计划中)都可以工作。只是现在在 Mac 和 Windows 7 上都运行的 FF5 在该页面上失败了。

Here's my conundrum:
I have a page that uses Google Maps V3 and jQuery. It all worked well locally in FF5, Chrome and Safari.

Once I uploaded to a web site, I get a "google is not defined" error on the first line that I try to use a google object

var defaultLocation = new google.maps.LatLng(lat, lng);

It only occurs in FF and only occurs remotely (i.e., if I load the file into FF locally, it works well). Chrome and Safari seem to be working great regardless, as is my Android and iPod browsers.

Here's what I tried so far:

  1. Moved <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> to top of the <head> section.
  2. Moved all content of $(function() {...}); to a function called initialize() and added <body onload="initialize()">
  3. Played with scripts and css files order
  4. Pasted the URL http://maps.google.com/maps/api/js?sensor=false into FF address box and verified I'm getting the legit script

But since this is only happening in FF on a remote machine and works well otherwise, I don't think it has anything to do with my code. Maybe the load order in FF5 is screwed. Maybe it prioritizes network resources differently than other browsers. I really do not know what to make of it at this point.

Any help is appreciated.
Guy

Update:
Just wanted to add the following fact: After trying the previous on a Mac, I tried FF5 in Windows, and have replicated the exact same behavior.
For good measure, I tried Pale Moon as well - same results. Chrome 14, Opera 11.50 and even frickin' IE9 (which wasn't included in the test plan) work. It just FF5, now on both Mac and Windows 7, that fails on that page.

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

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

发布评论

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

评论(15

蓝天 2024-11-26 14:30:52

尝试使用这个:

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

Try using this:

<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
陌若浮生 2024-11-26 14:30:52

试试这个:

<script src="https://maps.googleapis.com/maps/api/js"></script>

它对我有用......
重点是,将 HTTP 更改为 HTTPS

try this:

<script src="https://maps.googleapis.com/maps/api/js"></script>

it works for me...
the point is, change HTTP to HTTPS

℡Ms空城旧梦 2024-11-26 14:30:52

添加脚本的类型

<script src="https://maps.googleapis.com/maps/api/js" type="text/javascript"></script>

所以重要的部分是类型text/javascript。

Add the type for the script

<script src="https://maps.googleapis.com/maps/api/js" type="text/javascript"></script>

So the important part is the type text/javascript.

转身泪倾城 2024-11-26 14:30:52

而不是

var defaultLocation = new google.maps.LatLng(lat, lng);

使用

var defaultLocation = new window.google.maps.LatLng(lat, lng);

这个,这对我有用。

instead of this

var defaultLocation = new google.maps.LatLng(lat, lng);

use this

var defaultLocation = new window.google.maps.LatLng(lat, lng);

and this worked for me.

甜柠檬 2024-11-26 14:30:52

从 Firefox 23 开始,默认设置启用混合内容阻止(本地禁用)。如果您使用安全连接和一些不安全的 API,它也会阻止来自 Google 的一些 API。

要禁用它,当存在一些不安全内容时,您必须单击位置栏中出现的盾牌,设置“禁用保护”,然后您必须查看位置栏中的黄色感叹号:(

https://blog.mozilla.org/.../mixed-content-blocking-enabled-in-firefox-23/

您始终可以尝试将 http 协议替换为 https API url。如果 API 也在安全连接中提供 - 您将不会看到任何警告

From Firefox 23, there is Mixed Content Blocking Enabled set by default (locally disabled). It blocks some APIs from Google also if you use secure connection and some unsecure APIs.

To disable it you'll have to click shield which appears in location bar when there are some unsecure contents, set 'Disable protection' and then you'll have to look at yellow exclamation mark in location bar :(

https://blog.mozilla.org/.../mixed-content-blocking-enabled-in-firefox-23/

You can always try also replace http protocol with https in the API url. If API is provided also in secure connection - you will not see any warnings.

It works for me.

不可一世的女人 2024-11-26 14:30:52

您可以尝试以下操作:

首先,添加async defer。这指定脚本一旦可用且页面完成解析后将异步执行。

其次,将 initMap() 函数添加为 html 内脚本标记中的回调。这样,地图将在 document.ready 和 window.onload 之前初始化:

<script async defer src="{{ 'https://maps.googleapis.com/maps/api/js?key=$key&language='.$language.'®ion='.$country.'&callback=initMap' }}"></script>

<script>
    var map;
    function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
            center: {lat: -34.397, lng: 150.644},
            zoom: 4,
            disableDefaultUI: false,
            scrollwheel: false,
            styles: [{ ... }]
        });
    }
</script> 

最后,您可以在 js 文件中使用地图对象。

You can try the following:

First, add async defer. This specifies that the script will be executed asynchronously as soon as it is available and when the page has finished parsing.

Second, add the initMap() function as a callback in a script tag inside your html. In this way the map will be initialized before the document.ready and window.onload:

<script async defer src="{{ 'https://maps.googleapis.com/maps/api/js?key=$key&language='.$language.'®ion='.$country.'&callback=initMap' }}"></script>

<script>
    var map;
    function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
            center: {lat: -34.397, lng: 150.644},
            zoom: 4,
            disableDefaultUI: false,
            scrollwheel: false,
            styles: [{ ... }]
        });
    }
</script> 

Finally, you can use the map object inside your js files.

等风来 2024-11-26 14:30:52

更改

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API"> 
      function(){
            myMap()
                }
</script>

并使其

<script type="text/javascript">
      function(){
            myMap()
                }
</script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API"></script>

有效:)

Changed the

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API"> 
      function(){
            myMap()
                }
</script>

and made it

<script type="text/javascript">
      function(){
            myMap()
                }
</script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API"></script>

It worked :)

海的爱人是光 2024-11-26 14:30:52

我认为最简单的技巧是:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR API KEY&callback=initMap">google.maps.event.addDomListener(window,'load', initMap);</script>

当您的应用程序准备就绪时,它将初始化地图。

检查一下。

I think the easiest trick is:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR API KEY&callback=initMap">google.maps.event.addDomListener(window,'load', initMap);</script>

It will init the map when your app is ready.

Check this.

月朦胧 2024-11-26 14:30:52

请检查您调用的顺序、您的库、以下顺序

  1. <脚本类型 = "text/javascript" src = "../resources/googleMaps /jquery-ui.min.js">

  2. <脚本类型 = "text/javascript" src="http:// maps.googleapis.com/maps/api/

  3. METOD <脚本类型 = "text/javascript" src = "googleMaps/mapa.js">

我遇到了这个问题,我刚刚调整了我的订单。

Please check the order you are calling, your libraries, the following order

  1. <script type = "text/javascript" src = "../resources/googleMaps/jquery-ui.js"></script>

  2. <script type = "text/javascript" src = "../resources/googleMaps/jquery-ui.min.js"></script>

  3. <script type = "text/javascript" src="http://maps.googleapis.com/maps/api/

  4. METOD <script type = "text/javascript" src = "googleMaps/mapa.js"></script>

I was with this problem, I just adjusted my order.

夜吻♂芭芘 2024-11-26 14:30:52

就我而言,我通过 http 加载 google 脚本,而我的服务器有 SSL 并且通过 https 加载。所以我必须通过 https 加载脚本

In my case I was loading the google script via http while my server had SSL and its being loaded over https. So I had to load script via https

过期情话 2024-11-26 14:30:52

我不确定,但这是我最好的建议。

您正在使用 jQuery。因此,您实际上应该使用 $(function() {... }); 而不是设置事件进行初始化。使用此选项的原因是,它可以确保页面上的所有脚本都已加载,并且您不会像使用 onload body 标记那样仅限于一个 init 函数。

另外,请确保您的 Javascript 代码位于 Google 包含之后。否则,您的代码可能会在创建 Google 对象之前执行。

我还建议您查看一下有关事件顺序的页面。

http://dean.edwards.name/weblog/2005/09/busted/

I don't know for sure but here are my best suggestions.

You're using jQuery. So instead of setting the event you should really be using $(function() {... }); to do your initialization. The reason to use this is that it ensures that all the scripts on the page have loaded and you're not limited to just one init function like you are with the onload body tag.

Also, be sure your Javascript code is after the Google include. Otherwise your code might execute before the Google objects are created.

I would also suggest taking a look at this page about event order.

http://dean.edwards.name/weblog/2005/09/busted/

π浅易 2024-11-26 14:30:52

如果您在 API 调用中提到了回调函数,则应该将您的 google 地图代码包装在具有相同名称的 JavaScript 函数中。否则你会得到这个错误。

If you have mentioned a call back function in API call, you should wrap your google map code inside a JavaScript function with same name. Else you will get this error.

泡沫很甜 2024-11-26 14:30:51

我多次遇到“谷歌未定义”。
可能 Google Script 有一些问题,无法用 FF-addon 很好地加载
顺便提一句。
FF 有重新启动选项(如窗口重新启动)
帮助>禁用附加组件后重新启动

I faced 'google is not defined' several time.
Probably Google Script has some problem not to be loaded well with FF-addon
BTW.
FF has restart option ( like window reboot )
Help > restart with Add-ons Disabled

瞄了个咪的 2024-11-26 14:30:51

我在使用 Gmap3 时遇到了同样的错误“google 未定义”。问题是我在包含“google”之前包含了“gmap3”,所以我颠倒了顺序:

<script src="https://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script src="/assets/gmap3.js?body=1" type="text/javascript"></script>

I had the same error "google is not defined" while using Gmap3. The problem was that I was including 'gmap3' before including 'google', so I reversed the order:

<script src="https://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script src="/assets/gmap3.js?body=1" type="text/javascript"></script>
毁虫ゝ 2024-11-26 14:30:51

另一个对我有帮助的建议:

这是发生在我身上的事情=>我加载页面时,我的脚本每 3 次运行一次,错误是“google 未定义”。

我使用谷歌地图的函数是在我的 jQuery 文档的就绪函数中,

$(function(){
   //Here was my logic
})

我只是添加了这段代码以确保它有效:

$(function(){
   $(window).load(function(){
       //Here is my logic now
   });
});

它就像一个魅力。如果您想了解有关文档就绪和窗口加载之间差异的更多详细信息,这里有一篇很棒的文章:
window.onload 与 $(document).ready()

ready 事件发生在 HTML 文档加载之后,而
onload 事件稍后发生,此时所有内容(例如图像)也已
已加载。

onload事件是DOM中的标准事件,而ready事件
特定于 jQuery。准备事件的目的是
应该在文档加载后尽早发生,所以
向页面中的元素添加功能的代码不会
必须等待所有内容加载。

Another suggestion that helped me:

Here is what happent to me => My script was working once in 3 time I was loading the page and the error was the «google is not defined».

My function using the google map was in my jQuery document's ready function

$(function(){
   //Here was my logic
})

I simply added this code to make sure it works:

$(function(){
   $(window).load(function(){
       //Here is my logic now
   });
});

It works like a charm. If you want more details on difference between document ready and window load, here is a great post about it:
window.onload vs $(document).ready()

The ready event occurs after the HTML document has been loaded, while
the onload event occurs later, when all content (e.g. images) also has
been loaded.

The onload event is a standard event in the DOM, while the ready event
is specific to jQuery. The purpose of the ready event is that it
should occur as early as possible after the document has loaded, so
that code that adds functionality to the elements in the page doesn't
have to wait for all content to load.

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