|
7 | 7 | function geodesicPoly(Klass, fill) { |
8 | 8 | return Klass.extend({ |
9 | 9 |
|
| 10 | + options: { |
| 11 | + segmentsCoeff: 5000 |
| 12 | + }, |
| 13 | + |
10 | 14 | initialize: function (latlngs, options) { |
11 | 15 | Klass.prototype.initialize.call(this,latlngs,options); |
12 | 16 | this._geodesicConvert(); |
|
32 | 36 | }, |
33 | 37 |
|
34 | 38 | _geodesicConvert: function () { |
35 | | - this._latlngs = L.geodesicConvertLines(this._latlngsinit,fill); |
| 39 | + this._latlngs = geodesicConvertLines(this._latlngsinit,fill,this.options.segmentsCoeff); |
36 | 40 | } |
37 | 41 | }); |
38 | 42 | } |
|
41 | 45 | // as north/south lines have very little curvature in the projection, we can use longitude (east/west) seperation |
42 | 46 | // to calculate intermediate points. hopefully this will avoid the rounding issues seen in the full intermediate |
43 | 47 | // points code that have been seen |
44 | | - function geodesicConvertLine(startLatLng, endLatLng, convertedPoints) { |
| 48 | + function geodesicConvertLine(startLatLng, endLatLng, convertedPoints, segmentsCoeff) { |
45 | 49 |
|
46 | 50 | // maths based on https://edwilliams.org/avform.htm#Int |
47 | 51 |
|
|
52 | 56 |
|
53 | 57 | var dLng = lng2-lng1; |
54 | 58 |
|
55 | | - var segments = Math.floor(Math.abs(dLng * earthR / 5000)); |
| 59 | + segmentsCoeff = segmentsCoeff || 5000; |
| 60 | + var segments = Math.floor(Math.abs(dLng * earthR / segmentsCoeff)); |
56 | 61 |
|
57 | 62 | if (segments > 1) { |
58 | 63 | // pre-calculate some constant values for the loop |
|
79 | 84 | convertedPoints.push(L.latLng(endLatLng)); |
80 | 85 | } |
81 | 86 |
|
82 | | - |
83 | | - |
84 | | - L.geodesicConvertLines = function (latlngs, fill) { |
| 87 | + function geodesicConvertLines (latlngs, fill, segmentsCoeff) { |
85 | 88 | if (latlngs.length === 0) { |
86 | 89 | return []; |
87 | 90 | } |
|
108 | 111 | geodesiclatlngs.push(latlngs[0]); |
109 | 112 | } |
110 | 113 | 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); |
112 | 115 | } |
113 | 116 | if(fill) { |
114 | | - geodesicConvertLine(latlngs[len], latlngs[0], geodesiclatlngs); |
| 117 | + geodesicConvertLine(latlngs[len], latlngs[0], geodesiclatlngs, segmentsCoeff); |
115 | 118 | } |
116 | 119 |
|
117 | 120 | // now add back the offset subtracted above. no wrapping here - the drawing code handles |
|
120 | 123 | geodesiclatlngs = geodesiclatlngs.map(function (a) { return L.latLng(a.lat, a.lng+lngOffset); }); |
121 | 124 |
|
122 | 125 | return geodesiclatlngs; |
123 | | - }; |
| 126 | + } |
124 | 127 |
|
125 | 128 | L.GeodesicPolyline = geodesicPoly(L.Polyline, false); |
126 | 129 | L.GeodesicPolygon = geodesicPoly(L.Polygon, true); |
|
139 | 142 | }, |
140 | 143 |
|
141 | 144 | options: { |
| 145 | + segmentsCoeff: 1000, |
| 146 | + segmentsMin: 48, |
142 | 147 | fill: true |
143 | 148 | }, |
144 | 149 |
|
|
187 | 192 | return L.latLng(lat * r2d,lng * r2d); |
188 | 193 | }; |
189 | 194 |
|
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)); |
192 | 197 | //console.log(" (drawing circle as "+segments+" lines)"); |
193 | 198 | var points = []; |
194 | 199 | for (var i=0; i<segments; i++) { |
|
216 | 221 | return new L.GeodesicCircle(latlng, radius, options); |
217 | 222 | }; |
218 | 223 |
|
| 224 | + // L.geodesicConvertLines = geodesicConvertLines; |
219 | 225 | }()); |
0 commit comments