OpenLayers - how do I draw a Polygon from existing

2019-01-22 03:07发布

I have in my database longitude-latitude verticies from user-defined polygons. My questions is: how do I recreate and display them on a map now? This is quite easy to do with the Google Maps API, but I can't find any documentation or examples on how to do this with OpenLayers. Has anyone had any experience doing this?

标签: openlayers
1条回答
劫难
2楼-- · 2019-01-22 04:02

After a lot of experimenting, I found out how to do it:

var sitePoints = [];
var siteStyle = {
  // style_definition
};

var epsg4326 = new OpenLayers.Projection("EPSG:4326");
for (var i in coordinates) {
  var coord = coordinates[i];
  var point = new OpenLayers.Geometry.Point(coord.lng, coord.lat);
  // transform from WGS 1984 to Spherical Mercator
  point.transform(epsg4326, map.getProjectionObject());
  sitePoints.push(point);
}
sitePoints.push(sitePoints[0]);

var linearRing = new OpenLayers.Geometry.LinearRing(sitePoints);
var geometry = new OpenLayers.Geometry.Polygon([linearRing]);
var polygonFeature = new OpenLayers.Feature.Vector(geometry, null, siteStyle);
vectors.addFeatures([polygonFeature]);
查看更多
登录 后发表回答