2012-06-26 55 views
4

嗨我有一個函數,從xml數據獲取信息和在谷歌地圖上打印標記 我的問題是,我想創建一個點到另一個點之間的路徑 這是代碼該retrive數據
`在AJAX創建一個列表

$.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 
         });` 

所以我想補充我retrive到列表中的日期,並在此代碼畫一個線,如:

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)]將是動態的方式創建 感謝你的幫助

回答

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 
       }); 
       ... 
+0

非常感謝你:) – shaharnakash