2015-11-04 40 views
0

因此,我試圖從數據庫中查詢經緯度/長度的谷歌地圖上創建標記,並保存到XML文件。我試圖從XML文件中抓取位置並放入新的Google標記中,但無法在地圖上顯示任何標記。標記不會顯示在來自XML文件的谷歌地圖上

我的HTML/JavaScript的

//Test for markers 
<!DOCTYPE html> 
<html> 
    <head> 
    <style> 
     #map { 
     width: 1200px; 
     height: 900px; 
     } 
    </style> 
    <script src="https://maps.googleapis.com/maps/api/js"></script> 
    <script> 
    var map; 
     function initialize() { 
     var myLatlng = new google.maps.LatLng(38.0400, -122.7400); 
     var mapOptions = { 
      zoom: 10, 
      center: myLatlng 
     } 
     map = new google.maps.Map(document.getElementById("map"), mapOptions); 
     } 
     loadMarkers(); 
     google.maps.event.addDomListener(window, 'load', initialize); 

    function loadMarkers(){ 
     map.markers = map.markers || [] 
     downloadUrl("schools.xml", function(data) { 
      var xml = data.responseXML; 
      markers = xml.documentElement.getElementsByTagName("school"); 
      for(var i = 0; i < markers.length; i++){ 
       var name = markers[i].getAttribute("name"); 
       var point = new google.maps.LatLng(
       parseFloat(markers[i].getAttribute("lat")), 
       parseFloat(markers[i].getAttribute("long"))); 
       var marker = new google.maps.Marker({ 
        map:map, 
        position: point, 
        title:name 
       }); 
       map.markers.push(marker);    
      } 
     }); 
     } 

    function downloadUrl(url,callback) { 
     var request = window.ActiveXObject ? 
      new ActiveXObject('Microsoft.XMLHTTP') : 
      new XMLHttpRequest; 

     request.onreadystatechange = function() { 
      if (request.readyState == 4) { 
       request.onreadystatechange = doNothing; 
       callback(request, request.status); 
      } 
     }; 

     request.open('GET', url, true); 
     request.send(null); 
    } 
    </script> 
    </head> 
    <body onload = "initialize()"> 
    <div id="map"></div> 
    </body> 
</html> 

XML文件

<?xml version="1.0"?> 
-<Schools> 
<school loc="POINT (-122.549936 37.955014)" lat="37.955014" long="-122.549936" county="Marin" zip="94904" state="CA" city="Kentfield" name2="" name="College of Marin"/> 
<school loc="POINT (-122.564302 37.969594)" lat="37.969594" long="-122.564302" county="Marin" zip="94960" state="CA" city="San Anselmo" name2="" name="San Francisco Theological Seminary"/>  
<school loc="POINT (-122.514654 37.980014)" lat="37.980014" long="-122.514654" county="Marin" zip="94901" state="CA" city="San Rafael" name2="" name="Dominican University of California"/> 
</Schools> 
+0

可能是一個計時問題。我會將loadMarkers調用移入初始化函數中以開始。錯誤控制檯說什麼? – lucas

+0

@lucas我將loadMarkers修正爲初始化函數,但仍然沒有運氣。錯誤控制檯告訴我doNothing是未定義的 –

+0

這是因爲'doNothing'沒有被定義......這是'doNothing'的一個好的定義:'function doNothing(){}' – geocodezip

回答

0

你的代碼有一個JavaScript錯誤:Uncaught TypeError: Cannot read property 'markers' of undefined在這條線:

map.markers = map.markers || [] 

要調用前的loadMarkers地圖被定義。在初始化例程中調用它。

function initialize() { 
    var myLatlng = new google.maps.LatLng(38.0400, -122.7400); 
    var mapOptions = { 
    zoom: 10, 
    center: myLatlng 
    } 
    map = new google.maps.Map(document.getElementById("map"), mapOptions); 
    loadMarkers(); 
}