1

I've written code for plotting multiple linestring. I draw lines according to the distance entered and the number of lines. But in some cases, the lines start to disappear after a certain approximation to the line, but the main line never disappears. No operation or calculation and no errors. I couldn't figure out why.

The code example is as follows:

http://jsfiddle.net/OnurEk/ngv76ouL

What can be the reason for this and how can it be solved?

4
  • I can draw a line and each time a draw a new line , the previous line goes off. I can not really understand the question.........
    – pavlos
    Commented Jan 10, 2019 at 11:25
  • Normally the code draws by calculating the linestring that will occur in every map movement. But the lines drawn in a certain convergence disappear. No error. In the sample code, the green line is the main line and it does not disappear, but the lines outside it disappear when you zoom in. You can try to get closer to the lines on the code.
    – Onur Ek.
    Commented Jan 10, 2019 at 11:44
  • If you get close to one unit next to the red line, you will see the red line disappear, but you can get as close as you want to the main line (green line), not disappear. For the new fiddle you can try the convergence rate set on two lines. jsfiddle.net/ngv76ouL/1
    – Onur Ek.
    Commented Jan 11, 2019 at 15:03
  • I think I know your problem and how to solve it. Just hold on a sec to give you a proper answer.
    – pavlos
    Commented Jan 11, 2019 at 15:37

1 Answer 1

1

Well the problem is that when zooming in too much your main line is outside the viewport and so may not be visible. As a result the central line is not visible and thus your style goes off. This is why you may see all lines as long as main line (green one) is within ol viewport. A workaround to solve your issue is to use a renderBuffer when configuring your layer

So this piece of code:

var vectorLayer = new ol.layer.Vector({
    source: vectorSource,
    style:lineString
});

Should turn into this

 var vectorLayer = new ol.layer.Vector({
   source: vectorSource,
   renderBuffer:100000,//here I use 100000 pixels offset so make sure ol will draw the line event if it is 100000 pixels away from viewport
   style:lineString
 });

You may see the api docs here --> https://openlayers.org/en/v3.20.1/apidoc/ol.layer.Vector.html

The solution above should solve your issue but if you have many lines you would have performance issues. A proper solution, in my personal opinion, is to draw the extra lines to a vector layer and so they will always been visible, as long as they exist in the viewport, no matter the zoom level. In that way you will not force ol to draw lines 10000 pixels away from your viewport and thus you should not have any performance issues.

2
  • Thank you for your solution and help. So how do I make the code that they can always see as long as they're in the viewport, regardless of the level of synchronization and zooming of the extra lines to a vector layer? I'm new to OpenLayers so I didn't understand much.
    – Onur Ek.
    Commented Jan 12, 2019 at 16:21
  • Glad to help my friend. I need some time to write down your code as I would if it was my case. But unfortunately, I dont have much time these days, :(
    – pavlos
    Commented Jan 14, 2019 at 10:41

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.