Google地图上的多个多边形 - 无法正常工作 - 加入

时间:2012-05-14 14:01:44

标签: javascript google-maps

使用下面的代码,我可以在地图上绘制多边形,但是多边形被视为一个多边形,而api将它们连接起来,就像在多边形上一样。我一直在反对这个!我需要每个多边形在地图上加载不同的形状。在数组中格式如下:

[[11.11,11.11],[22.22,22.22],[33.33,33.33]]

[[[11.11,11.11],[22.22,22.22],[33.33,33.33]],[[11.11,11.11],[22.22,22.22],[33.33,33.33]]]




var polys = [[[51.4503639, -0.1006082], [51.4502851, -0.1000704], [51.4491791, -0.0992159], [51.4480629, -0.0978004], [51.4486121, -0.098658], [51.4503639, -0.1006082]], [[51.4225526, -0.1618125], [51.42297, -0.160525], [51.4222314, -0.1603534], [51.421847, -0.1590405], [51.4216169, -0.1583023], [51.4198131, -0.1587487], [51.4196633, -0.1579333], [51.4195509, -0.1571436], [51.4188674, -0.1577788], [51.4190548, -0.1585427], [51.4189852, -0.1594954], [51.4182427, -0.1601064], [51.4175419, -0.1606966], [51.416929, -0.1618334], [51.4175821, -0.1618763], [51.4183207, -0.1624428], [51.4181762, -0.1640049], [51.4182297, -0.1646916], [51.4191932, -0.1649233], [51.4204705, -0.1644732], [51.4211931, -0.1643445], [51.4216213, -0.1642758], [51.4209897, -0.1643016], [51.4201781, -0.163241], [51.4214072, -0.1623876], [51.4219531, -0.1621987], [51.4220602, -0.1621558], [51.4225526, -0.1618125]]]

  var latLng =[]
  var locs = []

  for (var _p=0; _p < polys.length; _p++) {
      locs = polys[_p];

      for (_i = 0, _len = locs.length; _i < _len; _i++) {
          point = locs[_i];
          latLng.push(new google.maps.LatLng(point[0], point[1]));}

          bermudaTriangle = new google.maps.Polygon({
           paths: latLng,
           fillColor: "#FF0000",
           fillOpacity:0.1
           });
  }
          bermudaTriangle.setMap(map);


  }

最终这起作用了:

  var latLng =[]
  var locs = []
  for (var _p=0; _p < polys.length; _p++) {
      locs = polys[_p];
      var  latLng = [];
      var bermudaTriangle = []
    for (_i = 0, _len = locs.length; _i < _len; _i++) {

            point = locs[_i];
            latLng.push(new google.maps.LatLng(point[0], point[1]));}


    bermudaTriangle = new google.maps.Polygon({
               paths: latLng,
               fillColor: "#FF0000",
               fillOpacity:0.1
    });

    bermudaTriangle.setMap(map);
     }

    }

1 个答案:

答案 0 :(得分:3)

您应该移动调用以在内循环之外创建多边形。而且每次都需要重新创建latLng数组。

var latLng =[];

for (_i = 0, _len = locs.length; _i < _len; _i++) {
          point = locs[_i];
          latLng.push(new google.maps.LatLng(point[0], point[1]));}
}

bermudaTriangle = new google.maps.Polygon({
           paths: latLng,
           fillColor: "#FF0000",
           fillOpacity:0.1
});

bermudaTriangle.setMap(map);