我如何绘制多个多边形。谷歌api

时间:2014-04-11 07:57:10

标签: api google-maps-api-3 polygon

我有几个coords数组,我需要绘制多个多边形

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]));
    };

1 个答案:

答案 0 :(得分: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