How to draw a polygon use open layer 3 programmatically?
i have a json array coordinate:
[
{
"lng": 106.972534,
"lat": -6.147714
},
{
"lng": 106.972519,
"lat": -6.133398
},
{
"lng": 106.972496,
"lat": -6.105892
}
]
and now i want to draw it on map use open layers. how to do it?
You need to use the
ol.geom.Polygon
constructor. That constructor expects an array of rings, each ring being an array of coordinates.In your case this is how you will create the polygon (this assumes your array of
lng
lat
pairs is nameda
):Now if you want to display this polygon in a map with a view whose projection is Web Mercator (
EPSG:3857
) you will need to transform the polygon fromEPSG:4326
toEPSG:3857
:And to actually display the polygon you need to wrap it in a feature object, and add it to a vector layer (a vector source really, see below), which you add to the map as any other layer: