2010-10-23 62 views
18

我在我的数据库中有来自用户定义多边形的经度 - 纬度顶点。我的问题是:我现在如何在地图上重新创建并显示它们?这对于Google Maps API来说非常简单,但我找不到任何有关如何使用OpenLayers执行此操作的文档或示例。有没有人有过这样的经验?OpenLayers - 如何从现有的lonLat点绘制多边形?

回答

34

大量实验后,我发现了如何做到这一点:

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]); 
+3

不知道有多少事情,因为已经更新,但不是行:'site_points.push(site_points [0] );''LinearRing'不需要,因为它们自动关闭自己? – dbmikus 2012-05-24 20:44:38

+0

可以请你分享如何在http://jsfiddle.net/上找到你。这将有很大的帮助。谢谢 – Profstyle 2015-12-11 09:06:27