使用xml文件中的折线绘制路径

时间:2012-11-19 17:41:12

标签: xml google-maps-api-3 google-polyline

我想使用从xml文件获取的坐标在Google地图上绘制路径。我有下一个代码,但它不起作用:

有谁知道怎么做?

最好的问候。

downloadUrl("myfile.asp", function(data) {
    var xml = xmlParse(data);
    var markers = xml.documentElement.getElementsByTagName("marker");
    var points = [];
    for (var i = 0; i < markers.length; i++) {
    var lat = parseFloat(markers[i].getAttribute("lat"));
    var lng = parseFloat(markers[i].getAttribute("lng"));
    var point = new google.maps.LatLng(lat,lng);

      }//finish loop

    var lineCoordinates = [ new google.maps.LatLng(point)];

     var path = new google.maps.Polyline({
path: lineCoordinates,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
 });
  path.setMap(map);

    }); //end download url

xml文件会保存这些数据:

<markers>
<marker imei="1887" lat="37.184872837428" lng="-3.86559306991389"/>
<marker imei="1887" lat="37.1853220115895" lng="-3.86580098489519"/>
<marker imei="1887" lat="37.1852771929778" lng="-3.86576667518516"/>
<marker imei="1887" lat="37.185537832391" lng="-3.86587104505817"/>
<marker imei="1887" lat="37.1852233612396" lng="-3.86573226263189"/>
<marker imei="1887" lat="37.1851605163743" lng="-3.86569774724824"/>
<marker imei="1887" lat="37.185510710715" lng="-3.86588200146997"/>
<marker imei="1887" lat="37.1855652010503" lng="-3.86582629326349"/>
<marker imei="1887" lat="37.1855560232803" lng="-3.86584872061447"/>
</markers>

1 个答案:

答案 0 :(得分:2)

您似乎缺少路径数组

downloadUrl("myfile.asp", function(data) {
    var xml = xmlParse(data);
    var markers = xml.documentElement.getElementsByTagName("marker");
    var path = [];
    for (var i = 0; i < markers.length; i++) {
      var lat = parseFloat(markers[i].getAttribute("lat"));
      var lng = parseFloat(markers[i].getAttribute("lng"));
      var point = new google.maps.LatLng(lat,lng);
      path.push(point);
    }//finish loop

    var polyline = new google.maps.Polyline({
      path: path,
      strokeColor: "#FF0000",
      strokeOpacity: 1.0,
      strokeWeight: 2
    });
    polyline.setMap(map);

}); //end download url

Working example that parses XML to polylines in v3