谷歌地图API - 谷歌没有定义的错误 [英] Google Maps API - Google Not Defined Error

查看:388
本文介绍了谷歌地图API - 谷歌没有定义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用谷歌地图API在我的骨干应用程序,但我得到一个错误。这里是我的code:

I am try to use the Google Maps API in my Backbone application, but I get an error. Here is my code:

var MyView = MyParrentView.extend({
    events: {
       //my events
    },
    initialize: function (options) {
        $('head').append('<script src="https://www.google.com/jsapi?key=MyAPIKey" type="text/javascript"></script>');  


   if (google != undefined) {
                google.load(
                    "earth",
                    "1",
                    {
                        "other_params":"sensor=false"
                    }
                );
            }
  }
 ,

如果(!谷歌=未定义){行,我得到的错误:的ReferenceError:没有定义谷歌。任何人都可以点我在正确的方向来解决这个问题或者为什么发生这种情况?我是pretty初次使用谷歌的API。

At if (google != undefined) { line, I get the error: ReferenceError: google is not defined. Can anyone point me in the right direction to fix this or why this is happening? I'm pretty new to using Google's API.

感谢

推荐答案

试试这个:直到脚本加载的回调将不会被执行

Try this: The callback won't be executed until the script is loaded.

var MyView = MyParrentView.extend({
    events: {
       //my events
    },
    initialize: function (options) {
        $.ajax({url: 'https://www.google.com/jsapi?key=MyAPIKey&language=EN&callback=onGoogleMapsLoaded',dataType: 'script'});
    },

    onGoogleMapsLoaded : function(){
        if (google != undefined) {
            google.load(
                "earth",
                "1",
                {
                    "other_params":"sensor=false"
                }
            );
        }
    }
}

这篇关于谷歌地图API - 谷歌没有定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆