From e6c0c9a51a626322f1bdf3a3329946f6445a8aa4 Mon Sep 17 00:00:00 2001 From: Jon Atkins Date: Thu, 21 Nov 2013 05:30:21 +0000 Subject: [PATCH] geodesic lines: improve the handling for lines that extend beyond the antimeridian. no odd straight line segments leaflet doesn't have particularly great handling for this, but this is a reasonable measure for now --- external/L.Geodesic.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/external/L.Geodesic.js b/external/L.Geodesic.js index 9d25b3b1..05a5f930 100644 --- a/external/L.Geodesic.js +++ b/external/L.Geodesic.js @@ -77,14 +77,28 @@ Modified by qnstie 2013-07-17 to maintain compatibility with Leaflet.draw } L.geodesicConvertLines = function (latlngs, fill) { - var i, j, len, geodesiclatlngs = []; - for (i = 0, len = latlngs.length; i < len; i++) { + if (latlngs.length == 0) { + return []; + } + + for (var i = 0, len = latlngs.length; i < len; i++) { if (L.Util.isArray(latlngs[i]) && typeof latlngs[i][0] !== 'number') { return; } latlngs[i] = L.latLng(latlngs[i]); } - + + // geodrsic calculations have issues when crossing the anti-meridian. so offset the points + // so this isn't an issue, then add back the offset afterwards + // a center longitude would be ideal - but the start point logitude will be 'good enougn' + var lngOffset = latlngs[0].lng; + + // points are wrapped after being offset relative to the first point coordinate, so they're + // within +-180 degrees + latlngs = latlngs.map(function(a){ return L.latLng(a.lat, a.lng-lngOffset).wrap(); }); + + var geodesiclatlngs = []; + if(!fill) { geodesiclatlngs.push(latlngs[0]); } @@ -94,6 +108,12 @@ Modified by qnstie 2013-07-17 to maintain compatibility with Leaflet.draw if(fill) { geodesicConvertLine(latlngs[len], latlngs[0], geodesiclatlngs); } + + // now add back the offset subtracted above. no wrapping here - the drawing code handles + // things better when there's no sudden jumps in coordinates. yes, lines will extend + // beyond +-180 degrees - but they won't be 'broken' + geodesiclatlngs = geodesiclatlngs.map(function(a){ return L.latLng(a.lat, a.lng+lngOffset); }); + return geodesiclatlngs; }