2014-12-05 31 views
0

我有一個顯示地圖的下面的函數。但是如何設置.addPolylines()函數中的循環。我獲得了正確的緯度&經度,但我添加了一個循環,但它顯示了一個空白的地圖。你能幫我解決嗎?添加循環以在Google地圖中迭代和繪製折線的最佳方式?

create_map(<%= raw @boundary_points.to_json %>);

function create_map(boundary_points) { 
    var handler = Gmaps.build('Google'); 

    handler.buildMap({ 
     internal: { 
      id: 'geolocation' 
     } 
    }, function() { 

     for (i = 1; i < boundary_points.length; i++) { 
      var ls = boundary_points[i].trim().split(" "); 
     } 

     var polylines = handler.addPolylines(
      [ 
       [ 
        for (i = 1; i < boundary_points.length; i++) { 
         var ls = boundary_points[i].trim().split(" "); { 
          lat: ls[0], 
          lng: ls[1] 
         }, 
        } 

       ] 
      ], { 
       strokeColor: '#FF0000' 
      } 
     ); 
     handler.bounds.extendWith(polylines); 
     handler.fitMapToBounds(); 
     handler.getMap().setZoom(15); 
     alert(boundary_points); 
    }); 
} 

回答

0

// 
 
// This is a sample array with points 
 
// 
 
var points = ["45.678295 -121.603813", "45.678196 -121.603607"]; 
 

 
// 
 
// This should be used for handler.addPolylines call 
 
// 
 
var polylines = []; 
 

 
for (i = 0; i < points.length; i++) { 
 
    var ls = points[i].trim().split(" "); 
 

 
    polylines[i] = {}; 
 
    polylines[i]["lat"] = parseFloat(ls[0]); 
 
    polylines[i]["lng"] = parseFloat(ls[1]); 
 
} 
 

 
console.log(polylines);

相關問題