我不是这样做的正确方法,但是OpenLayers只从多边形数组中绘制最后一个多边形。
这是我的代码:
for (...) {
var pointList = [];
var polygonList = [];
for (...) {
var newPoint = new OpenLayers.Geometry.Point(..X,...Y);
pointList.push(newPoint);
}
var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
var polygons = new OpenLayers.Geometry.Polygon([linearRing]);
polygonList.push(polygons);
}
var style_polygon2 = {
strokeColor: "#05fbff",
strokeOpacity: 1,
strokeWidth: 2,
fillColor: "#05fbff",
fillOpacity: 0.2
};
var multiPol = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.MultiPolygon(polygonList), null, style_polygon2);
...layer.addFeatures(multiPol);
如果有人做过这样的话,请告诉我......
谢谢!
答案 0 :(得分:5)
我已经测试了你的例子,在我的情况下一切正常。见LIVE DEMO。代码示例:
var options = {
div: "map",
zoom: 13,
center: [-9075004.4955698, 5028040.5259088],
layers: [
new OpenLayers.Layer.OSM()
]
},
source = [
[{x: -9075004, y: 5028040}, {x:-9079132, y: 5025403}, {x: -9072673, y: 5023568}],
[{x: -9074004, y: 5026040}, {x:-9073132, y: 5027403}, {x: -9074673, y: 5026568}],
[{x: -9073004, y: 5027040}, {x:-9072132, y: 5029403}, {x: -9075673, y: 5028568}]
],
polygonList = [],
multuPolygonGeometry,
multiPolygonFeature,
vector = new OpenLayers.Layer.Vector('multiPolygon'),
map = new OpenLayers.Map(options);
for (var i=0; i<source.length; i+=1) {
var pointList = [];
for (var j=0; j<source[i].length; j+=1) {
var point = new OpenLayers.Geometry.Point(source[i][j].x, source[i][j].y);
pointList.push(point);
}
var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
var polygon = new OpenLayers.Geometry.Polygon([linearRing]);
polygonList.push(polygon);
}
multuPolygonGeometry = new OpenLayers.Geometry.MultiPolygon(polygonList);
multiPolygonFeature = new OpenLayers.Feature.Vector(multuPolygonGeometry);
vector.addFeatures(multiPolygonFeature);
map.addLayer(vector);