2010-03-25 80 views
3

我按照api一步一步,但不能加載成功..爲什麼「谷歌是沒有定義」當我加載谷歌JS API

這是我的代碼:

<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
</head> 
<body > 
<script type="text/javascript" src="http://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22gdata%22%2C%22version%22%3A%222.x%22%2C%5B%22callback%22%3A%22onLoad%22%2C%22packages%22%3A%5B%22maps%22%5D%5D%7D%5D%7D"></script> 
<script type="text/javascript"> 
var service = new google.gdata.maps.MapsService('docs-example'); 
</script> 
</body> 
</html> 

錯誤是:google沒有定義

爲什麼??


upedted

我改變我的代碼,它的工作,但它火的ErrorHandler,然後警告 「錯誤!!!」:

<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
</head> 
<body > 
<script type="text/javascript" src="http://www.google.com/jsapi"></script> 
<script type="text/javascript"> 

    // Load the latest version of the Google Data JavaScript Client 
    google.load('gdata', '2.x', {packages: ['maps']}); 

    function onGoogleDataLoad() { 
    // Put your code here 
    } 

    // Call function once the client has loaded 
    google.setOnLoadCallback(onGoogleDataLoad); 

</script> 
<script type="text/javascript"> 
var service = new google.gdata.maps.MapsService('docs-example'); 
listMaps() 
function listMaps() { 
    var list = document.createElement('ul'); 
    var mapFeedUrl = 'http://maps.google.com/maps/feeds/maps/default/owned'; 

    service.getMapFeed(mapFeedUrl, function(feedRoot) { 
    var feed = feedRoot.feed; 
    var entries = feed.getEntries(); 
    for (var i = 0; i < entries.length; i++) { 
     var entry = entries[i]; 
     var mapTitle = entry.getTitle().getText(); 
     var listItem = document.createElement('li'); 
     var listText = document.createTextNode(mapTitle); 
     listItem.appendChild(listText); 
     list.appendChild(listItem); 
    } 
    document.body.appendChild(list); 
    }, errorHandler) 
} 
function errorHandler(){ 
    alert('error!!!') 
    } 
</script> 
</body> 
</html> 

爲什麼呢?

感謝

+0

您是否試過這個http://code.google.com/apis/maps/documentation/javascript/basics.html – 2011-12-07 04:58:54

回答

0

如果你與你的瀏覽器檢查JS網址,你會得到以下結果:

var error = new Error("Invalid autoload."); 
error.toString = function() { return this.message; } 
throw error; 

含義,有什麼東西不對您的網址。

1

您應該在onGoogleDataLoad()函數內調用Google的東西,以確保它在API加載完成之前不會啓動。

function onGoogleDataLoad() { 
    var service = new google.gdata.maps.MapsService('docs-example'); 
    listMaps() 
} 
+0

還提醒「錯誤!!!」 – zjm1126 2010-03-25 07:21:24