-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WrappedPolyline.js
55 lines (45 loc) · 1.35 KB
/
WrappedPolyline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* A class for displaying paths by parallels and meridians. Shouldn't be using for anything else.
*
* @class
* @extends L.Polyline
*/
L.WrappedPolyline = L.Polyline.extend(/** @lends L.WrappedPolyline.prototype */{
initialize: function (latlngs, options) {
L.Util.setOptions(this, options);
this.setLatLngs(latlngs);
},
setLatLngs: function (latlngs) {
this._originalLatLngs = [];
if (latlngs.length === 0) {
L.Polyline.prototype.setLatLngs.call(this, this._originalLatLngs);
return this;
}
let segments = [this._originalLatLngs, []], moveBy = 0, isFirstIteration = true;
for (let segment of segments) {
for (let i = 0; i < latlngs.length; i++) {
let point = L.latLng(latlngs[i]).clone();
if (isFirstIteration && !moveBy) {
if (point.lng < -180)
moveBy = 360;
else if (point.lng > 180)
moveBy = -360;
} else if (!isFirstIteration)
point.lng += moveBy;
segment.push(point);
}
isFirstIteration = false;
}
L.Polyline.prototype.setLatLngs.call(this, moveBy === 0 ? this._originalLatLngs : segments);
return this;
},
addLatLng: function (latLng) {
return this.setLatLngs([...this._originalLatLngs, latLng]);
},
getLatLngs: function () {
return this._originalLatLngs;
},
toGeoJSON: function (precision) {
return new L.Polyline(this._originalLatLngs).toGeoJSON(precision);
}
});