OpenLayers-如何从现有的lonLat点绘制多边形? [英] OpenLayers - how do I draw a Polygon from existing lonLat points?
本文介绍了OpenLayers-如何从现有的lonLat点绘制多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库中有来自用户定义的多边形的经度-纬度折点.我的问题是:现在如何重新创建并显示在地图上?使用Google Maps API可以很容易地做到这一点,但是我找不到有关如何使用OpenLayers做到这一点的任何文档或示例.有没有人有这样做的经验?
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?
推荐答案
经过大量实验,我发现了如何做:
After a lot of experimenting, I found out how to do it:
let sitePoints = [];
let siteStyle = {
// style_definition
};
let epsg4326 = new OpenLayers.Projection("EPSG:4326");
for (let i in coordinates) {
let coord = coordinates[i];
let 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]);
let linearRing = new OpenLayers.Geometry.LinearRing(sitePoints);
let geometry = new OpenLayers.Geometry.Polygon([linearRing]);
let polygonFeature = new OpenLayers.Feature.Vector(geometry, null, siteStyle);
vectors.addFeatures([polygonFeature]);
这篇关于OpenLayers-如何从现有的lonLat点绘制多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文