2015-08-22 32 views
1

我試圖構建一個應用程序,它可以構建多彩的pathlines(顏色取決於條件),並將它們顯示在小冊子地圖上。問題是以下。我有2個功能:一個是繪製區域,另一個是繪製區域的視圖。當我將latlan數組僅傳遞給一個時,它就完成了這項工作。當我把它傳遞給這兩個函數來繪製()& fitboduns()我得到一個(根據訂單)Mapbox/Leaflet Javascript:Uncaught TypeError:無法讀取未定義的屬性'lat'

  • :是否「遺漏的類型錯誤無法讀取屬性未定義的‘緯度’」的錯誤...如果我使用「addTo」方法綁定& draw-> line,則使用draw()函數。
  • 如果使用fitBounds方法繪製& bound-> line,bound()函數。

我試過很多東西,這是我最新的代碼

注:數據是L.latLng的數組:data.push(新L.latLng(測試[0],測試[1] ));

function draw(data) { 

var singlePath; 
singlePath = []; 

for (var i = 0; i < data.length; i++) { 

    singlePath.push(data[i],data[i+1]); 
    alert ("singlePath=" + singlePath); 
    var firstpolyline = L.polyline(singlePath, { 
    //color: pathColor[i] -> Color ARRAY 
    color : 'blue', 
    weight: 8, 
    opacity: 0.5, 
    smoothFactor: 1 

    }).addTo(leafmap); 
    singlePath = []; 
} 

function bound(data) { 
var data_bound =[]; 

var bounds = (new L.latLngBounds(data)); 

data_bound.push([bounds.getNorthEast().lat,bounds.getNorthEast().lng]); 
data_bound.push([bounds.getSouthWest().lat,bounds.getSouthWest().lng]); 
leafmap.fitBounds(data_bound); 

}

謝謝您的幫助!

回答

1
for (var i = 0; i < data.length; i++) { 
    singlePath.push(data[i],data[i+1]); 

這個循環結束於i == data.length - 1和您引用data[i + 1],這將是不確定的。將環路狀況更改爲i < data.length - 1

+0

非常感謝,有效! – Roud33

相關問題