2013-05-30 29 views
3

我想用點的二維數組來創建一個帶有Google Maps API的多邊形。這裏是我的代碼:創建多邊形時使用MVC數組

console.log(points) 
mvc = new google.maps.MVCArray(points) 
console.log(mvc.getArray()) 
console.log(mvc) 

poly = new google.maps.Polygon({ 
     paths: mvc, 
     strokeWeight: 2 
    }); 

//poly.setPaths(new google.maps.MVCArray(points));  

這裏是控制檯輸出看起來像

[[44.465332670616895, 26.143829190988], [44.466098355169805, 26.1465114000029], [44.4652867292244, 26.1474555375761], [44.4646435459323, 26.1463826539702], [44.4643066375701, 26.145588720101802]] 

[[44.465332670616895, 26.143829190988], [44.466098355169805, 26.1465114000029], [44.4652867292244, 26.1474555375761], [44.4646435459323, 26.1463826539702], [44.4643066375701, 26.145588720101802]] 

mg { b=[5], gm_accessors_={...}, length=5, more...} 

而這個錯誤:

Invalid value for constructor parameter 0: [object Object] 

的,我嘗試使用MVC陣列創建線多邊形,或將其分配爲多邊形路徑。

現在我做錯了什麼?

回答

2

分配給多邊形的路徑陣列的項目預計的LatLng對象,您必須首先轉換值:

for(var i=0; i < points.length; ++i){ 
    points[i] = new google.maps.LatLng(Number(points[i][0]), 
             Number(points[i][1])); 
}