从xml文件中选择元素

时间:2011-05-18 04:48:43

标签: javascript html xml google-maps

我的代码显示了shops.xml文件的所有地址。但我想在谷歌地图上显示与输入地址匹配的地址(在shops.php中)

shops.xml文件:

<shops>
<shop>
<name>Galaxy</name>
<address>72 Callander Road, Noble Park</address>
</shop>
<shop>
<name>Home</name>
<address>7 Close Avenue, Dandenong </address>
</shop>
</shops>

shop.htm:

<body>
<form> 
Ente Address: <input type="text" name="address" size="40" /> 
<input name="submit" type = "button" onClick = "load()" value = "Search"  /> 
</form>

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

shop.js

function load()
  {
    var  map = new GMap2(document.getElementById("map"));          
     map.setCenter(new GLatLng(-37.816667,144.966667), 10);      
     map.addControl(new GSmallMapControl());
     map.addControl(new GMapTypeControl());

    var  geocoder = new GClientGeocoder();

  GDownloadUrl("shops.xml", function(data) {
      var addr =document.getElementsByName['address']; 
      var xml = GXml.parse(data);
     var  shop = xml.documentElement.getElementsByTagName("shop");
      for (var i = 0; i < shop.length; i++) {
      var   addre= shop[i].getElementsByTagName("address");
        addre = addre[0].childNodes[0].nodeValue;
        if(addre==addr)
        var address=addre;
        geocoder.getLocations(address, addToMap);}
    });   
   }


function addToMap(response)
  {
  place = response.Placemark[0];
  point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

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

1 个答案:

答案 0 :(得分:0)

我认为你忘了把条件放在这个匹配地址的函数中。

- &GT; GDownloadUrl ...在这里你必须检查条件是否与xml地址匹配。
- &gt;在js变量中取值,如var addr = document.getElementsByName ['adr'];
- &GT;而不是传递geocoder.getLocations(address, addToMap);