2013-08-17 32 views
2

我到目前爲止所做的工作是使用By google documentation提供的指令解析JSON文件中的數據。我的問題是我需要改變我的代碼,以便我可以解析XML文件中的數據呢?如何解析xml座標到谷歌地圖上

此外我想添加內容到信息窗口。我在正確的軌道上?

我目前的代碼。

<!DOCTYPE html> 
    <html> 
     <head> 
     <style> 
      html, body, #map_canvas { margin: 0; padding: 0; height: 100%; } 
     </style> 
     <script 
      src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization"> 
     </script> 


     <script> 
     var map; 
     function initialize() { 
     var mapOptions = { 
     zoom: 5, 
     center: new google.maps.LatLng(-27.48939, 153.012772), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     map = new google.maps.Map(document.getElementById('map_canvas'), 
      mapOptions); 

     var script = document.createElement('script'); 
     script.src = 'http://earthquake.usgs.gov/earthquakes/feed/geojsonp/2.5/week'; 
     document.getElementsByTagName('head')[0].appendChild(script); 
    } 

    window.eqfeed_callback = function(results) { 
     for (var i = 0; i < results.features.length; i++) { 

     var earthquake = results.features[i]; 
     var coords = earthquake.geometry.coordinates; 

     var latLng = new google.maps.LatLng(coords[1],coords[0]); 

     var marker = new google.maps.Marker({ 
      position: latLng, 
      map: map, 

     }); 

     var infowindow = new google.maps.InfoWindow({ 
     content: "<div>Hello! World</div>", 
     maxWidth:100 
     }); 

     google.maps.event.addListener(marker, "mouseover", function() { 
      infowindow.open(this, marker); 
     }); 

     } 
    } 


     </script> 
     </head> 

     <body onload="initialize()"> 
     <div id="map_canvas"></div> 
     </body> 

     </html> 
+0

爲什麼你認爲你需要使用XML? – geocodezip

回答

0

My question is what do i need to change for my codes so that i can parse data out of a xml file instead ?

你不需要JSON改變爲XML。您可以使用從XML加載的數據執行任何操作,您可以使用從JSON加載的數據執行此操作。

+0

感謝geocodezip。再一個簡單的問題。我試圖從我的信息窗口的外部來源檢索動態內容信息。基於我在這裏得到的結果,我如何實現這一目標? '變種信息窗口=新google.maps.InfoWindow({'' 內容: 「

Hello! World
」,'' maxWidth:100' '});' 'google.maps.event.addListener(標記「 mouseover「,function()''{{ 'infowindow.open(this,marker); });' – user2691544

+0

Like [this](http://www.geocodezip.com/v3_SO_earthquakeMap.html)? – geocodezip

+0

是的。確實如此。 – user2691544