Search code examples
javascriptopenlayersopenlayers-3openlayers-5

LineString to latitude/longitude pairs OpenLayers


How can I get a collection of latitude/longitude pairs for a given LineString which may be in raw or compressed format in OpenLayers 6?

var draw; 
function addInteraction() {
var value ='LineString';
if (value !== 'None') {
draw = new Draw({
  source: source,
  type: 'LineString'
});
map.addInteraction(draw);
  }
}


 addInteraction();

Solution

  • You can read it on drawend event, something like this:

    draw.on('drawend', function (e) {
      let format = new ol.format.WKT();
      let wktRepresenation = format.writeGeometry(e.getGeometry(), {
        dataProjection: "EPSG:4326",
        featureProjection: "EPSG:3857",
      });
      console.log(wktRepresenation);
    });
    

    Change dataProjection and featureProjection depending on your needs.