4

I am rendering vector tiles and the roads are made up of many line segments. Each segment has a line layer and symbol layer in the vector tile.

I am getting repeated labels along main roads made up of many segments as shown below. Is there a way to space them out further?

I've tried using:-

padding: [100,100,100,100]

But this seems to have no effect. I'm using Openlayers 4 and have also tried in Openlayers 5 with same problem.

enter image description here

Style is essentially this:-

{padding: [100,100,100,100], text: "A19",  placement: "line", fill: ..., stroke: ..., font: ...}
1

I found a solution. It is not ideal but helps.

I created a global array

 window.labelcachearray = ['','','','','','','','','','',''];

In my style I then push label names to this array and return undefined if the name exists. I make sure the array shifts and this works well enough with repeated road names.

//prevent label repetition over last ten labels horrible hack 
positioning issue with road labels
        if(window.labelcachearray.includes(properties.name)){
           return undefined;
       }

        window.labelcachearray.unshift(properties.name);
        window.labelcachearray.pop();

I know! I know! I KNOW!!!! There are all sorts of issues but for UK road names it works and I cannot think of a better solution outside of hacking Openlayers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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