23

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?

Scott
  • 2,557
  • 5
  • 26
  • 32

2 Answers2

42

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]);
hanshenrik
  • 19,904
  • 4
  • 43
  • 89
Scott
  • 2,557
  • 5
  • 26
  • 32
  • 4
    Not sure how much things have been updated since, but isn't the line: `site_points.push(site_points[0]);` not necessary for `LinearRing`, since they automatically close themselves? – dbmikus May 24 '12 at 20:44
  • 1
    can you please share how you achived on http://jsfiddle.net/. that will help a lot. thanks – Profstyle Dec 11 '15 at 09:06
3

OpenLayers 6

it is slightly different for OpenLayers 6, and it took ma a while to figure it out. So I paste here the relevant code for OL6.

coordinates are of type [[[number]]] (which is the GeoJson standard for polygon).

styles is out-of-scope (i can paste it here if someone is interested, but every app can define it differently).

var VectorSource = ol.source.Vector;
var {Tile, Vector} = ol.layer; //import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
var TileLayer = Tile;
var VectorLayer = Vector;

var map = new ol.Map(...);

function drawPolygonOnMap(coordinates) {
    const polygonFeature = new ol.Feature(
        new ol.geom.Polygon(coordinates).transform('EPSG:4326','EPSG:3857'));


    let source = new VectorSource({
      features: [polygonFeature]
    });

    var layer = new VectorLayer({
      source: source,
      style: styles
    });

    map.addLayer(layer);
}
OhadR
  • 8,276
  • 3
  • 47
  • 53
  • 1
    I'm trying the following `new Polygon([[5724580.263018008, 2915911.362231543], [5724954.871637587, 2916119.9031961635], [5724619.881135941, 2915429.991834908], [5724580.263018008, 2915911.362231543]]).transform('EPSG:4326', 'EPSG:3857')` and for some reason it's still now drawing, (The weird thing is if idraw it after getting the coordinates from draw polygon it works, even though it's the same values) any idea ? – Dany Y Sep 24 '20 at 17:09
  • I discovered that the polygon constructor parameter is actually of type [[[]],...]. Can you try new Polygon([[[5724580.263018008, 2915911.362231543], [5724954.871637587, 2916119.9031961635], [5724619.881135941, 2915429.991834908], [5724580.263018008, 2915911.362231543]]]) i.e. with 3 square brackets? – Carl in 't Veld Jun 21 '23 at 20:40