微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从AJAX中创建javascript列表

我有一个功能,从谷歌地图上的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)]
                                       });`

我想要行路径:[new google.maps.LatLng(33.240547551860935,35.61381738496402)]将以动态方式创建
  谢谢你的帮助

解决方法

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

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

$.ajax({
            type:"POST",success: function(xml){
                // Parses the data from xml
                var newLat,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,animation: google.maps.Animation.DROP
                    });
                ...
                });
                mapLine = new google.maps.polyline({map : map,path:path
                });
                ...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐