[OpenLayers-Users] Can OpenLayers APIs merge line features

Stephen Woodbridge woodbri at swoodbridge.com
Wed Sep 2 00:20:10 EDT 2009


Kevin Yang wrote:
> Hello everyone,
>     A problem troubled me for a long time. Can OpenLayers APIs merge line 
> features ?I use this function to solve the following problem: When I
> calculate the shortest path, I get the WKTs of the LineStrings, but it is
> not arranged  in sequence.So I cannot use the marker' MoveTo method to
> implement the effct of a  little car picture going along the shortest path
> in sequence from start-node to end-node. Thus I want to consult you that Can
> OpenLayers APIs merge line  features ,and how to do it, or is there other
> ways to solve this problem properly? Thank you very much!
> best regards,
> Yang

I had a similar problem and was using pgRouting. The way I solved it was 
to have my Ajax server the gets the results from pgRouting do some 
additional checking and flip the segments so they are all in the same 
direction along the path. Basically, the segments are returned in the 
order of first to last, but because you might traverse a segment in the 
opposite direction than that which it is stored in the database you get 
the segment in reverse order. Your segments are all stored with A as the 
start and B as the end. Then you might get results back like:

1  2  3  4  5  6
AB,AB,BA,AB,BA,BA,etc

So you have to first see if the first segment needs to be flipped:

if 1A == 2A or 1A == 2B then flip first segment,
for i=2 through N {
   if (i-1)B == iB then flip segment i
}

now all the segments should be in the correct end to end order. and you 
can just chain them all together.

I'm sure you can do the same in Javascript.

-Steve W.



More information about the Users mailing list