javascript中的问题

时间:2011-05-13 23:25:05

标签: javascript html xml

我正在使用以下代码,这里我显示的是地址 在谷歌地图上带有标记的properties.xml。它没有给我任何错误,但也没有在谷歌地图上显示任何内容。

有人能告诉我代码中的问题在哪里吗?

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 个答案:

答案 0 :(得分: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;
}

createMarker应用于map.addOverlay

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