2011-05-13 55 views
0

我使用下面的代碼,在這裏我顯示 properties.xml在Google地圖上標記的地址。它給了我沒有錯誤,但也沒有在谷歌地圖上顯示。問題在javascript

有人可以告訴我我的代碼中的問題在哪裏?

properties.xml文件:

<properties> 
<property> 
<type>apartment</type> 
<address>538 Little Lonsdale Street,Melbourne,VIC </address> 
</property> 
<property> 
<type>apartment</type> 
<address>196 Little Lonsdale Street,Melbourne,VIC</address> 
</property> 
<property> 
<type>apartment</type> 
<address>5/5 Close Ave,Dandenong,VIC </address> 
</property> 
</properties> 

HTML文件:

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
<title>property</title> 
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAB1CHU9LrppxpqwUwaxkvTBRIez7zTAJDrX7CdEV86wzEyVzTHBQKDxL9qiopaHZkOJ8F2t0RQW9W8A" 
     type="text/javascript"></script> 
<script type="text/javascript" src="map.js"></script> 
</head> 

<body onload="load()" onunload="GUnload()"> 

<div id="map" style="width: 500px; height: 400px"></div> 

</body> 
</html> 

map.js文件:

var map; 
    var geocoder; 
    var xml; 
    var property; 
    var address; 

    function load() 
    { 

     map = new GMap2(document.getElementById("map"));   
    map.setCenter(new GLatLng(-24.994167,134.866944), 4); 

     map.addControl(new GSmallMapControl()); 
     map.addControl(new GMapTypeControl()); 
     geocoder = new GClientGeocoder(); 

     GDownloadUrl("../../data/properties.xml", function(data) { 
      xml = GXml.parse(data); 
      property = xml.documentElement.getElementsByTagName("property"); 
      for (var i = 0; i < property.length; i++) { 
      address = property[i].getElementsByTagName("address"); 
      address = address.firstChild.nodeValue; 

      geocoder.getLocations(address, addToMap);} 
     }); 
    } 


    function addToMap(response) 
    { 

     place = response.Placemark[0]; 

     point = new GLatLng(place.Point.coordinates[1], 
          place.Point.coordinates[0]); 

     marker = new GMarker(point); 
     map.addOverlay(marker); 


    } 

回答

1

我想你的代碼,並顯示了在第一次加載地圖但有一個JS錯誤。這是這一行:

address = address.firstChild.nodeValue; 

應該是這個讓裏面的文字<address>

address = address[0].childNodes[0].nodeValue; 

這裏是地圖的一部分時,它的工作:

Here is a portion of the map that I saw when it worked

只是提及我起初看不到任何標記,因此我將其替換爲:

map.setCenter(new GLatLng(37.997022, -122.6), 11); 

有:

map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 

不知道,如果這就是你期待什麼,但就是這樣發展的結果。

* UPDATE * 要創建您可以使用此功能

function createMarker(point, address) 
{ 
    var marker = new GMarker(point); 
    GEvent.addListener(marker, "click", function() { 
    map.openInfoWindowHtml(point, address); 
    }); 
    return marker; 
} 

應用標記的createMarkermap.addOverlay

map.addOverlay(createMarker(point, response.name)); 
+0

我用烏爾第一碼childnode值,但沒有出現在地圖上。但是當我嘗試使用map.setCenter的第二個代碼時,所有內容都消失了,只是灰色的空框 – ash 2011-05-14 01:06:57

+0

@ash你是在服務器上還是在本地文件中查看你的頁面?確保在服務器上運行它以避免域限制。如果它正確,也檢查XML的位置。我能夠看到加載的地圖,當我嘗試它時,我唯一需要解決的問題是JS錯誤。 – tradyblix 2011-05-14 01:10:08

+0

我在服務器上查看。我可以在運行代碼時看到加載的地圖,但它不顯示地圖上帶有標記的properties.xml文件的地址? – ash 2011-05-14 01:21:53