1

我正在升级到谷歌地图的 v3 并且在加载我的折线时遇到了一些问题。我的代码基于此链接中给出的答案:使用 xml 文件中的折线绘制路径,并带有几个变体以匹配我的 XML 文件。

downloadUrl("byway.xml", function(doc) {
    var xmlDoc = xmlParse(doc);
        var trks = xmlDoc.documentElement.getElementsByTagName("trk");
        var bywaypath = [];
        for (var i = 0; i < trks.length; i++) {
            var trkPoints = trks[i].getElementsByTagName("trkpt");
            for (var j = 0; j<trkPoints.length; j++) {
    var lat = parseFloat(trkPoints[j].getAttribute("lat"));
  var lng = parseFloat(trkPoints[j].getAttribute("lng"));
  var point = new google.maps.LatLng(lat,lng);
  bywaypath.push(point);
            }
        }


  var myPath = new google.maps.Polyline({
  path: bywaypath,
  strokeColor: "#008eb0",
  strokeOpacity: 1.0,
  strokeWeight: 3
});

myPath.setMap(地图); });

这是我的 XML 文件的样子:

<gpx version="1.1" creator="RouteBuddy" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">

<wpt lat="30.23041000" lon="-88.02368200">
<ele>935.481445</ele>
<trk>
    <name>ACC</name>
    <trkseg>

<trkpt lat="    30.23047759 " lon=" -88.02365995    "></trkpt>
<trkpt lat="    30.23040677 " lon=" -88.02368044    "></trkpt>
<trkpt lat="    30.23041    " lon=" -88.023682  "></trkpt>
<trkpt lat="    30.230062   " lon=" -88.023445  "></trkpt>
<trkpt lat="    30.229971   " lon=" -88.023339  "></trkpt>
<trkpt lat="    30.229628   " lon=" -88.022956  "></trkpt>
<trkpt lat="    30.229628   " lon=" -88.022956  "></trkpt>
<trkpt lat="    30.22975762 " lon=" -88.02288814    "></trkpt>
<trkpt lat="    30.22973402 " lon=" -88.02284032    "></trkpt>
<trkpt lat="    30.22973402 " lon=" -88.02284032    "></trkpt>

关于我做错了什么的任何想法?提前致谢!

4

1 回答 1

0
  1. 你的 XML 有效吗?
  2. 这看起来不一致(您的代码正在寻找属性“lng”,XML 包含属性“lon”:

    var lng = parseFloat(trkPoints[j].getAttribute("lng"));
    
    
    <trkpt lat="    30.23047759 " lon=" -88.02365995    "></trkpt>
    
于 2013-05-15T15:45:13.207 回答