Can anyone help me to convert this function so that it is not dependant on the ol3-debug file as I don't know quite what is replaced upon build. The function works but only in debug mode.

It is in reference to this question: Sketch event in Openlayers 3 drawing interaction

The intention is that it will allow me to display each features co-ords as they are drawn on-screen.

geometryFunction: function (c, g) {
        if (goog.isDef(g)) {
        } else {
            g = new ol.geom.LineString(c);
        if (c[0].length > ct) {
            console.log('click coord : ' + c[0][c[0].length - 1]);
            ct = c[0].length;
        } else {
            console.log('move coord : ' + c[0][c[0].length - 1]);

        return g;

1 Answer 1


You can use:

var draw = new ol.interaction.Draw({
    source: vectorSource,
    type: "LineString",
    geometryFunction: function(coords, geom) {
        if (!geom) {
            geom = new ol.geom.LineString(null);
        return geom;

See https://gis.stackexchange.com/a/152646/50718


  • Thanks Jonatas, that makes perfect sense. Unfortunately I still can't implement this in the way I need. I currently have a function triggered by the drawend event which will print out a list of the coordinates transformed into lon lat pairs. When I try to incorporate this into thegeometry function it replicates each value approx. 4 times. I think I need to register each click event but I don't know how to do this. My example is below.
    – dvmac01
    Oct 12, 2015 at 11:32
  • lns.on('drawend', function (e) { var lnsPoints = []; for (var i = 0; coords.length > i; i++) { var coord = coords[i]; var coordTrans = ol.proj.transform(coord, 'EPSG:3857', 'EPSG:4326'); lnsPoints.push(coordTrans); } for (var i = 0; lnsPoints.length > i; i++) { var pointPair = lnsPoints[i]; var lon = pointPair[1]; var lat = pointPair[0]; var pointList = "html to build list"; $('#lns_p').append(pointList); } });
    – dvmac01
    Oct 12, 2015 at 11:39
  • See fiddle - jsfiddle.net/jonataswalker/8b322285 Oct 12, 2015 at 12:00
  • Again, many thanks for your attention Jonatas. This is pretty much working as it should now except that the double-click 'drawend' event will duplicate the last coordinates. Is it possible to 'catch' the drawend event within the geometry function? I've adapted your fiddle to illustrate: jsfiddle.net/dvmac/ughxwm4n
    – dvmac01
    Oct 12, 2015 at 15:17
  • In fact I've just stumbled upon your fiddle: jsfiddle.net/jonataswalker/ots444of regarding appending coordinates. This is exactly the kind of functionality I'm looking for so I'm wondering if I should start with an empty linestring on my layer and append coords and push to my ul simultaneously?
    – dvmac01
    Oct 12, 2015 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.