The problem is when drawing a path across the map the points that should be on the right appears on the left:
I tried to fix it by checking whether the distance between longitudes of two consequent points of the path is greater than PI, and in that case adding PI to a point with the least longitude, but it doesn't seem to work:
const latlngs = points.map(pt => new L.LatLng(pt[1], pt[0]))
for (let i = 0; i < latlngs.length - 1; i++) {
let [minLatLng, maxLatLng] = [latlngs[i], latlngs[i+1]];
if (minLatLng.lng > maxLatLng.lng) {
[minLatLng, maxLatLng] = [maxLatLng, minLatLng];
}
if (maxLatLng.lng - minLatLng.lng > Math.PI) {
minLatLng.lng += Math.PI
}
const polyline = new L.Polyline([latlngs[i], latlngs[i+1]], {
color: 'red',
weight: 3,
opacity: 0.5,
smoothFactor: 1,
});
polyline.addTo(map);
}