来自XML的Google Map Polyline阵列

时间:2014-11-07 19:14:33

标签: javascript php google-maps polyline

在我的javascript代码中,每x秒,我从XML获得一个变量'polys',具有无限数量的坐标对,因为这(我省略了一些变量):echo.php

<markers>
<marker iph="ce987" polys="[40.66645296,-0.22793904],[39.66973274,0.21806265]"/>
</markers>

Javascript代码:

        function processXML(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        //clear markers before you start drawing new ones
        resetMarkers(markersArray)
        for (var i = 0; i < markers.length; i++) {
            var idp = markers[i].getAttribute("idp");
            var polys = markers[i].getAttribute("polys");

            var genpolys = [];

            var flightPath = new google.maps.Polyline({
              path: genpolys,
              geodesic: true,
              strokeColor: '#FF0000',
              strokeOpacity: 1.0,
              strokeWeight: 2,
              map: map
            });

            markersArray.push(marker);
        }
        setTimeout(function() {
            downloadUrl("echo.php", processXML);
        }, 19000);
    }

而且,我需要生成一个¿数组?与coordenates,像这样:

var genpolys = [
    new google.maps.LatLng(40.66645296,-0.22793904),
    new google.maps.LatLng(39.66973274,0.21806265)
  ];

我的javascript级别太低,所以我不知道自动生成这个变量无限数量的坐标对。然后在'Polyline'中的'path'中使用

有人可以帮助我吗?任何建议,将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:3)

尝试这样的事情。 (我知道它不完全正确,但可能会给你一个想法。我会稍后尝试编辑。)

function processXML(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");

    //clear markers before you start drawing new ones
    resetMarkers(markersArray);

    for (var i = 0; i < markers.length; i++) {
        var idp = markers[i].getAttribute("idp");
        var polys = markers[i].getAttribute("polys");

        var genpolys = [];
        var json = JSON.parse('{"polys":['+polys+']}');
        for(var coord in json.polys){
            genpolys[coord] = new google.maps.LatLng(json.polys[coord][0],json.polys[coord][1]);    
        }

        var flightPath = new google.maps.Polyline({
        path: genpolys,
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2,
        map: map
        });

        markersArray.push(marker);
    }

    setTimeout(function() {
        downloadUrl("echo.php", processXML);
    }, 19000);
}

我希望这有助于你弄明白。我调整了此页面的答案:how to draw polylines on google maps dynamically