Stacked Lines (or Multilines?) between two Points on Map
Asked Answered
F

2

5

I hope that I am in the right place with my concern.

I recently made myself some thoughts on how my company software could evolve to get better and noticed on a website a map-based software, which had some kind of "stacked lines" between to points on the map. I know that it is possible to create a single line between two points, but IMHO the "stacked lines" are a good option for a map as well. Is it possible to create such an "multi line" example between two points with openlayers?

Here are two examples of what I mean:

Picture for Example one

Picture for Example two

My Question is not about getting a 100% answer! So i dont see the "offtopic" reason in here... it is more a question if anyone has tried to get something like this before or if it is possible to create such LineStrings from OL3 with the framework itself!

Fevre answered 9/11, 2015 at 13:36 Comment(0)
P
6

OpenLayers does not support this out-of-the box, but it provides the tools to build these "stacked-lines" yourself.

I gave it a quick try, you can find a demo here: http://jsfiddle.net/a06ufx8z/2/

  new ol.style.Style({
    geometry: function(feature) {
        var line = feature.getGeometry();
        var coords = [];
        line.forEachSegment(function(from, to) {
            // for each segment calculate a parallel segment with a
            // distance of 4 pixels
            var angle = Math.atan2(to[1] - from[1], to[0] - from[0]);
            var dist = 4 * resolution;
            var newFrom = [
                Math.sin(angle) * dist + from[0],
                -Math.cos(angle) * dist + from[1]
            ];
            var newTo = [
                Math.sin(angle) * dist + to[0],
                -Math.cos(angle) * dist + to[1]
            ];
            coords.push(newFrom);
            coords.push(newTo);
        });

        return new ol.geom.LineString(coords);
    },
    stroke: ...
})

The idea is to calculate a parallel line for each segment of your line-string with a fixed distance. I "borrowed" the math to calculate the parallel line from this answer.

This demo has a small problem which you can see at point 2 and 3. You can avoid this by calculating the intersection between two consecutive segments.

Placate answered 11/11, 2015 at 9:50 Comment(2)
Thanks for the example, i already tried myself with another solution i will also post here... but your example looks nice aswell and it is just a "question of own liking". Btw: will the recalculation of the LineStrings be put on the map, so that they all together will move to the center of the point (e.g. if theres an icon-image, will they all "enter" the point on the image? Or will they have an offset from left / right)?Fevre
Sorry, I don't understand your question. Could you maybe reformulate?Placate
F
3

Already got it working by myself, a but more complicated than the first answer though:

My server renders a JSON as response. In this response it is included between which two points which lines should be shown (also with color and weight). so i go through each object in this array... the single objects are orderd by "weight" descending, so that the thickest line would be added first to the array, all other lines afterwards. This method will show the thickest line on the "Bottom", the 2nd thickest line over the first etc etc. That's how i made it possible to show several lines between two points (like shown in the example).

Fevre answered 11/11, 2015 at 13:15 Comment(1)
Could you provide above explaination with code...i tried but it's only working for two lineString,in other word for first and lastLinestring and in between linestring style is overriding by last lineString.Haskell

© 2022 - 2024 — McMap. All rights reserved.