Given is a LineString, a start and an end point. A parallel to this LineString should be plotted (using openlayers). Please have a look at the image for an example (the black line is the parallel, the blue line the original line, start/end points aren't plotted).
To achive this I set a custom renderer within the style of the vector layer (ol/style/Style~RenderFunction
). This function takes the pixel coordinates of the original line, calculates the parallel and plot it into the canvas.
Problem: The created parallel line can't be selected by the user by e.g. clicking on it when using ol/interaction/Select~Select
. I want to provide information about the feature to user as soon as selected.
Question: How is it possible to make this feature selectable? Are there any additional steps necessary within the renderer-function?
The custom renderer function looks like that:
function parallelRenderer(pixelCoordinates, state) {
const parallelCoordinates = computeParallelCoordinates(pixelCoordinates);
const canvas = state.context;
canvas.beginPath();
canvas.moveTo(parallelCoordinates[0][0], parallelCoordinates[0][1]);
for (let i=1; i<parallelCoordinates.length; i++) {
canvas.lineTo(parallelCoordinates[i][0], parallelCoordinates[i][1]);
}
canvas.stroke();
}
Background:
- Start/end points are created by the user using
ol/interactions/Draw
- The created line is selectable when providing
ol/style/Stroke~Stroke
instead of the renderer function - I'm using openlayers version 6.5.0
Update: There is a related question (and also the answer of Mike in this thread). The difference is, that real world coordinates are used there to compute the position of the parallel line. I wanted to solve that in pixel space... The reason is that I want to keep the distance between the lines constant. Using real world coordinates the distance is dependent on the resolution. It seems that this problem can be solved by recalculating the real world geometry every time the resolution changes.