openlayersopenlayers-6angular-openlayers

How to draw the multyline in OpenLayers?


I have learned a Draw object in Openlayers.

I try to draw the dotted line when user moves the mouse. When user clicks the last and prev lines should be replaced on straight line.

How does it work, should I use two different drawing ayers and switch between them?

Or I can retrieve a last line from draw object and redraw it on straight line?

I try to use this sample from official document:

https://openlayers.org/en/latest/examples/measure-style.html


Solution

  • There is a solution: measure-style

    1. Set a tag when feature draw end
    draw.on('drawend', function (e) {
        e.feature.set('finished',true)
        ...
    });
    
    1. Rewrite style function to treat the two situations differently
    function styleFunction(feature, segments, drawType, tip) {
      const stroke = style.getStroke()
      if (feature.get('finished'))
        stroke.setLineDash(null)
      else 
        stroke.setLineDash([10,10])
    ...
    }