javascript - 从 AJAX 在 javascript 中创建列表

标签 javascript html ajax

你好,我有一个函数可以从 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)] 将以动态方式创建 谢谢你的帮助

最佳答案

构建数组: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
                });
                ...

关于javascript - 从 AJAX 在 javascript 中创建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11204272/

相关文章:

javascript - 如何用公司图标替换页面加载器

javascript - ng-click 不在 Ionic 中触发

javascript - 可拖动元素上的 z-index 堆叠问题(jQuery UI)

html - 谷歌地图未显示在 div 中

javascript - JWT API token 存储

javascript - 如何在angularjs中向div添加元素?

html - DIV 在嵌入文档上的绝对定位

javascript - 使用 JavaScript/AJAX 将行插入数据库

javascript - Ajax POST 数据未发送到 ASP.NET 中的 Page_Load 方法

php - 如何在没有表单方法或操作的情况下将输入值保存在明显显示的输入字段中