Skip to content

Commit 0224c15

Browse files
author
johndoe
committed
replace precision-related constants with options
So it's now possible to override values, e.g.: ``` L.GeodesicPolyline.mergeOptions({ // default: segmentsCoeff: 500 // 5000 }); L.GeodesicPolygon.mergeOptions({ segmentsCoeff: 500 // 5000 }); L.GeodesicCircle.mergeOptions({ segmentsCoeff: 500, // 1000 segmentsMin: 96 // 48 }); ```
1 parent bbbf446 commit 0224c15

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

src/L.Geodesic.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
function geodesicPoly(Klass, fill) {
88
return Klass.extend({
99

10+
options: {
11+
segmentsCoeff: 5000
12+
},
13+
1014
initialize: function (latlngs, options) {
1115
Klass.prototype.initialize.call(this,latlngs,options);
1216
this._geodesicConvert();
@@ -32,7 +36,7 @@
3236
},
3337

3438
_geodesicConvert: function () {
35-
this._latlngs = L.geodesicConvertLines(this._latlngsinit,fill);
39+
this._latlngs = geodesicConvertLines(this._latlngsinit,fill,this.options.segmentsCoeff);
3640
}
3741
});
3842
}
@@ -41,7 +45,7 @@
4145
// as north/south lines have very little curvature in the projection, we can use longitude (east/west) seperation
4246
// to calculate intermediate points. hopefully this will avoid the rounding issues seen in the full intermediate
4347
// points code that have been seen
44-
function geodesicConvertLine(startLatLng, endLatLng, convertedPoints) {
48+
function geodesicConvertLine(startLatLng, endLatLng, convertedPoints, segmentsCoeff) {
4549

4650
// maths based on https://edwilliams.org/avform.htm#Int
4751

@@ -52,7 +56,8 @@
5256

5357
var dLng = lng2-lng1;
5458

55-
var segments = Math.floor(Math.abs(dLng * earthR / 5000));
59+
segmentsCoeff = segmentsCoeff || 5000;
60+
var segments = Math.floor(Math.abs(dLng * earthR / segmentsCoeff));
5661

5762
if (segments > 1) {
5863
// pre-calculate some constant values for the loop
@@ -79,9 +84,7 @@
7984
convertedPoints.push(L.latLng(endLatLng));
8085
}
8186

82-
83-
84-
L.geodesicConvertLines = function (latlngs, fill) {
87+
function geodesicConvertLines (latlngs, fill, segmentsCoeff) {
8588
if (latlngs.length === 0) {
8689
return [];
8790
}
@@ -108,10 +111,10 @@
108111
geodesiclatlngs.push(latlngs[0]);
109112
}
110113
for (i = 0, len = latlngs.length - 1; i < len; i++) {
111-
geodesicConvertLine(latlngs[i], latlngs[i+1], geodesiclatlngs);
114+
geodesicConvertLine(latlngs[i], latlngs[i+1], geodesiclatlngs, segmentsCoeff);
112115
}
113116
if(fill) {
114-
geodesicConvertLine(latlngs[len], latlngs[0], geodesiclatlngs);
117+
geodesicConvertLine(latlngs[len], latlngs[0], geodesiclatlngs, segmentsCoeff);
115118
}
116119

117120
// now add back the offset subtracted above. no wrapping here - the drawing code handles
@@ -120,7 +123,7 @@
120123
geodesiclatlngs = geodesiclatlngs.map(function (a) { return L.latLng(a.lat, a.lng+lngOffset); });
121124

122125
return geodesiclatlngs;
123-
};
126+
}
124127

125128
L.GeodesicPolyline = geodesicPoly(L.Polyline, false);
126129
L.GeodesicPolygon = geodesicPoly(L.Polygon, true);
@@ -139,6 +142,8 @@
139142
},
140143

141144
options: {
145+
segmentsCoeff: 1000,
146+
segmentsMin: 48,
142147
fill: true
143148
},
144149

@@ -187,8 +192,8 @@
187192
return L.latLng(lat * r2d,lng * r2d);
188193
};
189194

190-
191-
var segments = Math.max(48,Math.floor(this._radius/1000));
195+
var o = this.options;
196+
var segments = Math.max(o.segmentsMin,Math.floor(this._radius/o.segmentsCoeff));
192197
//console.log(" (drawing circle as "+segments+" lines)");
193198
var points = [];
194199
for (var i=0; i<segments; i++) {
@@ -216,4 +221,5 @@
216221
return new L.GeodesicCircle(latlng, radius, options);
217222
};
218223

224+
// L.geodesicConvertLines = geodesicConvertLines;
219225
}());

0 commit comments

Comments
 (0)