Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could do something like this:</p> <p><a href="http://jsfiddle.net/hZXUK/2/" rel="nofollow">DEMO</a></p> <pre><code>var start = { end_lat: 1, end_lng: 1 }, end = { start_lat: 4, start_lng: 4 }, coordsBetween = [ { start_lat: 2, start_lng: 2, end_lat: 3, end_lng: 3 }, { start_lat: 1, start_lng: 1, end_lat: 2, end_lng: 2 }, { start_lat: 3, start_lng: 3, end_lat: 4, end_lng: 4 } ]; function orderedCoords(start, coords) { var result = [], point = start; while (point = coords.filter(function (item) { return item.start_lat === point.end_lat &amp;&amp; item.start_lng === point.end_lng; })[0]) { result.push(point); } return result; } console.log(orderedCoords(start, coordsBetween)); </code></pre> <p>Basically we find the next <code>point</code> that starts where <code>start</code> ends and let that <code>point</code> become the next <code>start</code> until there's no match and at each step we push the point into <code>result</code>.</p> <p>EDIT:</p> <blockquote> <p>This would work of the coordinates of the start and end point overlapped, but none of mine do, there is a large gap between them and I need to generate the 'connectors' between them...</p> </blockquote> <p>I have expanded my first idea by using an <a href="http://www.purplemath.com/modules/distform.htm" rel="nofollow">algorithm to calculate the closest point</a> insead of looking for overlapping coords.</p> <p><a href="http://jsfiddle.net/hZXUK/4/" rel="nofollow">DEMO</a></p> <pre><code>var start = { end_lat: 1, end_lng: 1 }, end = { start_lat: 4, start_lng: 4 }, segments = [ { start_lat: 2, start_lng: 2, end_lat: 3, end_lng: 3 }, { start_lat: 1, start_lng: 1, end_lat: 2, end_lng: 2 }, { start_lat: 3, start_lng: 3, end_lat: 4, end_lng: 4 } ]; function orderedSegments(start, segments) { var result = [], segment = start, i; while ((i = indexOfClosestSegment(segment, segments)) !== -1) { result.push(segment = segments.splice(i, 1)[0]); } return result; } function indexOfClosestSegment(segment, segments) { var i = 0, len = segments.length, segIndex = -1, tempDistance, smallestDistance; for (; i &lt; len; i++) { if ( (tempDistance = distanceBetween(segment, segments[i])) &lt; smallestDistance || typeof smallestDistance === 'undefined') { smallestDistance = tempDistance; segIndex = i; } } return segIndex; } function distanceBetween(segmentA, segmentB) { return Math.sqrt( Math.pow(segmentB.start_lat - segmentA.end_lat, 2) + Math.pow(segmentB.start_lng - segmentA.end_lng, 2) ); } console.log(orderedSegments(start, segments)); </code></pre> <blockquote> <p><strong>Notice that the points are geo coordinates and you will need to use a spherical distance algorithm, not the trivial pythagoras. I think GM provides such helper functions for their data structures; of course the algorithm will still work with a different distanceBetween implementation. – <em>@Bergi</em></strong></p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload