2013-11-14 66 views
1

我遇到了一个奇怪的错误。我试图加载谷歌地球库,但这样做时,我得到错误“ERR_CREATE_PLUGIN”Google地球“ERR_CREATE_PLUGIN”

下面的代码不工作:

<script src="http://www.google.com/jsapi"></script> 
<script> 
    google.load("earth", "1"); 

    var ge = null; 

    function init() { 
     google.earth.createInstance("map3d", initCallback, failureCallback); 
    } 

    function initCallback(object) { 
     ge = object; 
     ge.getWindow().setVisibility(true); 
    } 

    function failureCallback(object) { 
    } 
</script> 
</head> 
<body onload='init()' id='body'> 
    <center> 
     <div id='map3d' 
      style='border: 1px solid silver; height: 600px; width: 800px;'></div> 
    </center> 
</body> 

尽管此代码不:

<script type="text/javascript"> 
    google.load("earth", "1"); 

    var ge = null; 

    function initCallback(object) { 
     ge = object; 
     ge.getWindow().setVisibility(true); 
    } 

    function failureCallback(object) { 
    } 

    $(document).ready(function() { 


     google.earth.createInstance("map3d", initCallback, failureCallback);  
    }); 
</script> 

回答

3

不能工作的原因是因为jquery可能会在Google Earth API之前加载。

google.earth.createInstance()google.load()已完成之前被jquery调用$(document).ready()

为确保在调用createInstance()之前正确加载所有内容 - 只需从Google loader通过google.load()方法加载jquery和地球api。这样,您就可以使用setOnLoadCallback方法知道什么时候一切准备就绪。即

<script type="text/javascript" src="http://www.google.com/jsapi"></script> 
<script type="text/javascript"> 
    google.load("jquery", "1"); 
    google.load("earth", "1"); 
    google.setOnLoadCallback(function() { 
    //Place init code here instead of $(document).ready() 
    google.earth.createInstance("map3d", initCallback, failureCallback); 
    }); 

    // etc...