Javascript网页期望至少加载地图,但实际上无法加载任何内容

时间:2012-01-15 13:57:11

标签: javascript google-maps-api-3

下面是嵌入了Javascript的html页面。 Map API密钥对我的网络域有效,只有当我隐藏所有Javascript函数并将initialize()留在那里时,页面才能加载地图,就像Google的官方示例代码一样,否则我什么也看不见页面加载后(即空白页面) 有人能帮我找到原因吗?

谢谢!

<html>   
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Real-time iphone tracker</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAzcrhlFUxvQMBI3qYkY87zhStzVKTwp4tXCAZpL7xj82pGpEoixSAvciDeL2RO_S1xLoGgxKBpbG6HQ&sensor=true"
        type="text/javascript"></script>
<script type="text/javascript">

var xmlobj;
function createXMLHttpRequest(){
   if(window.ActiveXObject)
   {
       return new ActiveXObject("Microsoft.XMLHTTP");
   }

   else if(window.XMLHttpRequest)
   {
       return = new XMLHttpRequest();
   }
}

function sendRequest(){

    xmlobj = createXMLHttpRequest();
    if(!xmlobj){
        alert('The browser is not compatible with XMLHttpRequest');
        return 0;
    }
    var indexurl = "index.php";
    xmlobj.onreadystatechange = stateHandler;
    xmlobj.open("GET", indexurl, true);
    xmlobj.send(null);

}
function stateHandler(){
  if(xmlobj.readyState == 4 && xmlobj.status == 200){
    document.getElementById("map_data").innerHTML= xmlobj.responseText;
  }
}

function initialize() {
  if (GBrowserIsCompatible()) {
    sendRequest();
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(22.37664, 114.184902), 13);
    map.setUIToDefault();

    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();
    for (var i = 0; i < 10; i++) {
      var point = new GLatLng(southWest.lat() + latSpan * Math.random(),
                              southWest.lng() + lngSpan * Math.random());
      map.addOverlay(new GMarker(point));
    }
  }
}
</script>
</head>
<body onload="initialize()" >
<div id="map_canvas" style="width: 500px; height: 300px"></div>
<br>
<div> id="map_data" ></div>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

您的JavaScript中存在错误。 createXMLHttpRequest应该是这样的:

function createXMLHttpRequest(){
   if(window.ActiveXObject)
   {
       return new ActiveXObject("Microsoft.XMLHTTP");
   }

   else if(window.XMLHttpRequest)
   {
       return new XMLHttpRequest();
   }
}

请注意您写的:

return = new XMLHttpRequest();