2014-04-11 76 views
0

我有COORDS的幾個陣列,以及我需要吸引超過1個多邊形如何繪製多於一個的多邊形。谷歌API

function testarr() 
{ 
     polygon1 = [new google.maps.LatLng(54.66092513983568, 39.71935272216797), 
     new google.maps.LatLng(54.63847909506459, 39.718666076660156), 
     new google.maps.LatLng(54.63748562083948, 39.77668762207031), 
     new google.maps.LatLng(54.66430090574764, 39.77668762207031)]; 

     polygon2 = [new google.maps.LatLng(54.605482774276126, 39.713172912597656), 
     new google.maps.LatLng(54.58817883247707, 39.714202880859375), 
     new google.maps.LatLng(54.58917351104145, 39.734458923339844), 
     new google.maps.LatLng(54.607073572207575, 39.7320556640625)]; 

     polygonpolygon.push(polygon1); 
     polygonpolygon.push(polygon2); 
} 

如何創建新座標新的多邊形,如果我不知道有多少個多邊形需要什麼?

我嘗試這一點,但它了錯誤:

var polyg = new google.maps.Polygon({ 
     fillColor: 'blue', 
     path: polyarr111, 
     strokeWeight: 3 
     }); 

    for (var u=0; u<2; u++) { 
     polyg.setPaths(new google.maps.MVCArray(polygonpolygon[u])); 
    }; 

回答

0

你推你的路到一個數組polygonpolygon這樣你就可以循環通過它來創建你的多邊形。

var polygonOptions = { 
    fillColor: 'blue', 
    strokeWeight: 3 
}; 

for (var u=0; u<polygonpolygon.length; u++) { 
    var polygon = new google.maps.Polygon(polygonOptions); 
    polygon.setPaths(polygonpolygon[u]); 
    polygon.setMap(map); 
}; 

這樣它會創建任意數量的數組在你的數組中。

Working demo