当我加载google js api时,为什么“谷歌没有定义”

时间:2010-03-25 06:30:16

标签: javascript google-maps

我一步一步地遵循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>

错误是:谷歌未定义

为什么??


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>

为什么?

感谢

2 个答案:

答案 0 :(得分:1)

你应该对onGoogleDataLoad()函数中的google内容进行任何调用,以确保在API加载完成之前没有启动它。

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

答案 1 :(得分:0)

如果您使用浏览器检查JS URL,则会得到以下结果:

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

意思是,您的网址存在问题。