4


嗨,我有一个从 xml 数据获取信息并在谷歌地图上打印标记的函数我的问题是我想在一个点到另一点之间创建一条路径这是检索数据的代码

  $.ajax({
                type:"POST",
                url:"PipeServlet?op=1",
                dataType:"xml",
                success: function(xml){
                    // Parses the data from xml
                    var newLat, newLon, newDesc,newName;
                    $(xml).find("deal").each(function(){
                        newName = $(this).find("name").text();
                        newLat = $(this).find("lat").text();
                        newLon = $(this).find("lon").text();
                        newDesc = $(this).find("desc").text();
                        // Displaying the Coupons on the map
                        marker = new google.maps.Marker({
                            position: new google.maps.LatLng(newLat,newLon),
                            map: map,
                            title: newName,
                            html: newDesc,
                            animation: google.maps.Animation.DROP
                        });`

所以我想将我检索到的日期添加到列表中并在此代码中画一条线:

mapLine = new google.maps.Polyline({map : map,
                                        strokeColor   : '#ff0000',
                                        strokeOpacity : 0.6,
                                        strokeWeight  : 4,
                                        path:[new google.maps.LatLng(33.240547551860935,35.6153623373566),new google.maps.LatLng(33.240009149357576,35.61381738496402)]
                                       });`

我希望 path:[new google.maps.LatLng(33.240547551860935,35.6153623373566),new google.maps.LatLng(33.240009149357576,35.61381738496402)]以动态方式创建线路感谢您的帮助

4

1 回答 1

2

构建一个数组:var path = new Array();

并在其末尾添加您的对象:path.push(position);

$.ajax({
            type:"POST",
            url:"PipeServlet?op=1",
            dataType:"xml",
            success: function(xml){
                // Parses the data from xml
                var newLat, newLon, newDesc,newName;
                var path = new Array();
                $(xml).find("deal").each(function(){
                    newName = $(this).find("name").text();
                    newLat = $(this).find("lat").text();
                    newLon = $(this).find("lon").text();
                    newDesc = $(this).find("desc").text();
                    var position = new google.maps.LatLng(newLat,newLon);
                    path.push(position);
                    // Displaying the Coupons on the map
                    marker = new google.maps.Marker({
                        position: position,
                        map: map,
                        title: newName,
                        html: newDesc,
                        animation: google.maps.Animation.DROP
                    });
                ...
                });
                mapLine = new google.maps.Polyline({map : map,
                                strokeColor   : '#ff0000',
                                strokeOpacity : 0.6,
                                strokeWeight  : 4,
                                path:path
                });
                ...
于 2012-06-26T09:51:05.460 回答