@raruto/leaflet-elevation 2.2.5 → 2.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -674
- package/dist/leaflet-elevation.css +337 -337
- package/dist/leaflet-elevation.js +281 -280
- package/dist/leaflet-elevation.js.map +1 -1
- package/dist/leaflet-elevation.min.js +1 -1
- package/dist/leaflet-elevation.min.js.map +1 -1
- package/images/elevation-position.svg +1 -1
- package/images/elevation-pushpin.svg +1 -1
- package/images/elevation.svg +1 -1
- package/libs/leaflet-distance-marker.css +8 -8
- package/libs/leaflet-distance-marker.js +314 -314
- package/package.json +71 -71
- package/src/components/chart.js +783 -783
- package/src/components/d3.js +641 -641
- package/src/components/marker.js +96 -96
- package/src/components/summary.js +43 -43
- package/src/control.js +3 -2
- package/src/handlers/acceleration.js +64 -64
- package/src/handlers/heart.js +52 -52
- package/src/handlers/pace.js +65 -65
- package/src/handlers/slope.js +79 -79
- package/src/handlers/speed.js +66 -66
- package/src/handlers/time.js +102 -102
- package/src/index.css +329 -329
- package/src/index.js +42 -42
- package/src/options.js +85 -85
- package/src/utils.js +158 -158
|
@@ -1,314 +1,314 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2022, GPL-3.0+ Project, Raruto
|
|
3
|
-
*
|
|
4
|
-
* This file is free software: you may copy, redistribute and/or modify it
|
|
5
|
-
* under the terms of the GNU General Public License as published by the
|
|
6
|
-
* Free Software Foundation, either version 2 of the License, or (at your
|
|
7
|
-
* option) any later version.
|
|
8
|
-
*
|
|
9
|
-
* This file is distributed in the hope that it will be useful, but
|
|
10
|
-
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
-
* General Public License for more details.
|
|
13
|
-
*
|
|
14
|
-
* You should have received a copy of the GNU General Public License
|
|
15
|
-
* along with this program. If not, see .
|
|
16
|
-
*
|
|
17
|
-
* This file incorporates work covered by the following copyright and
|
|
18
|
-
* permission notice:
|
|
19
|
-
*
|
|
20
|
-
* Copyright (c) 2014- Doroszlai Attila, 2016- Phil Whitehurst
|
|
21
|
-
*
|
|
22
|
-
* Permission to use, copy, modify, and/or distribute this software
|
|
23
|
-
* for any purpose with or without fee is hereby granted, provided
|
|
24
|
-
* that the above copyright notice and this permission notice appear
|
|
25
|
-
* in all copies.
|
|
26
|
-
*
|
|
27
|
-
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
28
|
-
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
29
|
-
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
30
|
-
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
|
|
31
|
-
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
32
|
-
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
33
|
-
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
34
|
-
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
// TODO: the "L.DistanceMarker" (canvas marker) class could be alternatively provided by "leaflet-rotate"?
|
|
38
|
-
|
|
39
|
-
L.DistanceMarker = L.CircleMarker.extend({
|
|
40
|
-
_updatePath: function () {
|
|
41
|
-
let ctx = this._renderer._ctx;
|
|
42
|
-
let p = this._point;
|
|
43
|
-
|
|
44
|
-
// Calculate image direction (rotation)
|
|
45
|
-
this.options.rotation = this.options.rotation || 0;
|
|
46
|
-
|
|
47
|
-
// Draw circle marker (canvas point)
|
|
48
|
-
if (this.options.radius && this._renderer._updateCircle) {
|
|
49
|
-
this._renderer._updateCircle(this);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Draw image over circle (distance marker)
|
|
53
|
-
if (this.options.icon && this.options.icon.url) {
|
|
54
|
-
if (!this.options.icon.element) {
|
|
55
|
-
const icon = document.createElement('img');
|
|
56
|
-
this.options.icon = L.extend({ rotate: 0, size: [40, 40], offset: { x: 0, y: 0 } }, this.options.icon);
|
|
57
|
-
this.options.icon.rotate += this.options.rotation;
|
|
58
|
-
this.options.icon.element = icon;
|
|
59
|
-
icon.src = this.options.icon.url;
|
|
60
|
-
icon.onload = () => this.redraw();
|
|
61
|
-
icon.onerror = () => this.options.icon = null;
|
|
62
|
-
} else {
|
|
63
|
-
const icon = this.options.icon;
|
|
64
|
-
let cx = p.x + icon.offset.x;
|
|
65
|
-
let cy = p.y + icon.offset.y;
|
|
66
|
-
ctx.save();
|
|
67
|
-
if (icon.rotate) {
|
|
68
|
-
ctx.translate(p.x, p.y);
|
|
69
|
-
ctx.rotate(icon.rotate);
|
|
70
|
-
cx = 0;
|
|
71
|
-
cy = 0;
|
|
72
|
-
}
|
|
73
|
-
ctx.drawImage(icon.element, cx - icon.size[0] / 2, cy - icon.size[1] / 2, icon.size[0], icon.size[1]);
|
|
74
|
-
ctx.restore();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Add a label inside the circle (distance marker)
|
|
79
|
-
if (this.options.label) {
|
|
80
|
-
let cx = p.x, cy = p.y;
|
|
81
|
-
ctx.save();
|
|
82
|
-
|
|
83
|
-
ctx.font = this.options.font || 'normal 7pt "Helvetica Neue", Arial, Helvetica, sans-serif';
|
|
84
|
-
ctx.textAlign = "center";
|
|
85
|
-
ctx.textBaseline = "middle";
|
|
86
|
-
ctx.fillStyle = this.options.fillStyle || 'black';
|
|
87
|
-
|
|
88
|
-
// TODO rescale circle to fit text
|
|
89
|
-
// let fontSize = Number(/[0-9\.]+/.exec(ctx.font)[0]);
|
|
90
|
-
// let fontWidth = ctx.measureText(this.options.html).width;
|
|
91
|
-
|
|
92
|
-
if (this.options.rotation) {
|
|
93
|
-
ctx.translate(p.x, p.y);
|
|
94
|
-
ctx.rotate(this.options.rotation);
|
|
95
|
-
cx = 0;
|
|
96
|
-
cy = 0;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Temporary fix to prevent stroke blurs at higher zoom levels
|
|
100
|
-
if (this._map.getZoom() > 17) {
|
|
101
|
-
ctx.fillStyle = this.options.strokeStyle || 'black';
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
ctx.fillText(this.options.label, cx, cy);
|
|
105
|
-
|
|
106
|
-
if (this.options.strokeStyle && this._map.getZoom() <= 17) {
|
|
107
|
-
ctx.strokeStyle = this.options.strokeStyle;
|
|
108
|
-
ctx.strokeText(this.options.label, cx, cy);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
ctx.restore();
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
L.DistanceMarkers = L.LayerGroup.extend({
|
|
117
|
-
options: {
|
|
118
|
-
cssClass: 'dist-marker',
|
|
119
|
-
iconSize: [12, 12],
|
|
120
|
-
arrowSize: [10, 10],
|
|
121
|
-
arrowUrl: "data:image/svg+xml,%3Csvg transform='rotate(90)' xmlns='http://www.w3.org/2000/svg' width='560px' height='560px' viewBox='0 0 560 560'%3E%3Cpath stroke-width='35' fill='%23000' stroke='%23FFF' d='M280,40L522,525L280,420L38,525z'/%3E%3C/svg%3E",
|
|
122
|
-
offset: 1000,
|
|
123
|
-
showAll: 12,
|
|
124
|
-
textFunction: (distance, i, offset) => i,
|
|
125
|
-
distance: true,
|
|
126
|
-
direction: true,
|
|
127
|
-
},
|
|
128
|
-
initialize: function (line, map, options) {
|
|
129
|
-
|
|
130
|
-
this._layers = {};
|
|
131
|
-
this._zoomLayers = {};
|
|
132
|
-
|
|
133
|
-
options = L.setOptions(this, options);
|
|
134
|
-
|
|
135
|
-
let preferCanvas = map.options.preferCanvas;
|
|
136
|
-
let showAll = Math.min(map.getMaxZoom(), options.showAll);
|
|
137
|
-
|
|
138
|
-
// You should use "leaflet-rotate" to show rotated arrow markers (preferCanvas: false)
|
|
139
|
-
if (!preferCanvas && !map.options.rotate) {
|
|
140
|
-
console.warn('Missing dependency: "leaflet-rotate"');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Get line coords as an array
|
|
144
|
-
let coords = typeof line.getLatLngs == 'function' ? line.getLatLngs() : line;
|
|
145
|
-
|
|
146
|
-
// Handle "MultiLineString" features
|
|
147
|
-
coords = L.LineUtil.isFlat(coords) ? [coords] : coords;
|
|
148
|
-
|
|
149
|
-
coords.forEach(latlngs => {
|
|
150
|
-
// Get accumulated line lengths as well as overall length
|
|
151
|
-
let accumulated = L.GeometryUtil.accumulatedLengths(latlngs);
|
|
152
|
-
let length = accumulated.length > 0 ? accumulated[accumulated.length - 1] : 0;
|
|
153
|
-
|
|
154
|
-
// count = Number of distance markers to be added
|
|
155
|
-
// j = Position in accumulated line length array
|
|
156
|
-
for (let i = 1, count = Math.floor(length / options.offset), j = 0; i <= count; ++i) {
|
|
157
|
-
|
|
158
|
-
let distance = options.offset * i;
|
|
159
|
-
|
|
160
|
-
// Find the first accumulated distance that is greater than the distance of this marker
|
|
161
|
-
while (j < accumulated.length - 1 && accumulated[j] < distance) ++j;
|
|
162
|
-
|
|
163
|
-
// Grab two nearest points either side marker position
|
|
164
|
-
let p1 = latlngs[j - 1];
|
|
165
|
-
let p2 = latlngs[j];
|
|
166
|
-
let m_line = L.polyline([p1, p2]);
|
|
167
|
-
|
|
168
|
-
// and create a simple line to interpolate on
|
|
169
|
-
let ratio = (distance - accumulated[j - 1]) / (accumulated[j] - accumulated[j - 1]);
|
|
170
|
-
let position = L.GeometryUtil.interpolateOnLine(map, m_line, ratio);
|
|
171
|
-
let delta = map.project(p2).subtract(map.project(p1));
|
|
172
|
-
let angle = Math.atan2(delta.y, delta.x);
|
|
173
|
-
|
|
174
|
-
// Generate distance marker label
|
|
175
|
-
let text = options.textFunction.call(this, distance, i, options.offset);
|
|
176
|
-
|
|
177
|
-
// Grouping layer of visible layers at zoom level (arrow + distance)
|
|
178
|
-
let zoom = this._minimumZoomLevelForItem(i, showAll);
|
|
179
|
-
let markers = this._zoomLayers[zoom] = this._zoomLayers[zoom] || L.layerGroup()
|
|
180
|
-
|
|
181
|
-
// create arrow markers
|
|
182
|
-
if (options.direction && ((options.distance && i % 2 == 1) || !options.distance)) {
|
|
183
|
-
if (preferCanvas) {
|
|
184
|
-
markers.addLayer(
|
|
185
|
-
new L.DistanceMarker(p1, {
|
|
186
|
-
radius: 0,
|
|
187
|
-
icon: {
|
|
188
|
-
url: options.arrowUrl, //image link
|
|
189
|
-
size: options.arrowSize, //image size ( default [40, 40] )
|
|
190
|
-
rotate: 0, //image base rotate ( default 0 )
|
|
191
|
-
offset: { x: 0, y: 0 }, //image offset ( default { x: 0, y: 0 } )
|
|
192
|
-
},
|
|
193
|
-
rotation: angle,
|
|
194
|
-
interactive: false,
|
|
195
|
-
// label: '⮞', //'➜',
|
|
196
|
-
// font: 'normal 20pt "Helvetica Neue", Arial, Helvetica, sans-serif',
|
|
197
|
-
// fillStyle: 'white',//'#3366CC',
|
|
198
|
-
// strokeStyle: 'black',
|
|
199
|
-
})
|
|
200
|
-
);
|
|
201
|
-
} else {
|
|
202
|
-
markers.addLayer(
|
|
203
|
-
L.marker(position.latLng, {
|
|
204
|
-
icon: L.icon({
|
|
205
|
-
iconUrl: options.arrowUrl,
|
|
206
|
-
iconSize: options.arrowSize,
|
|
207
|
-
}),
|
|
208
|
-
// NB the following option is added by "leaflet-rotate"
|
|
209
|
-
rotation: angle * L.DomUtil.RAD_TO_DEG,
|
|
210
|
-
interactive: false,
|
|
211
|
-
})
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// create distance markers
|
|
217
|
-
if (options.distance && i % 2 == 0) {
|
|
218
|
-
if (preferCanvas) {
|
|
219
|
-
markers.addLayer(
|
|
220
|
-
new L.DistanceMarker(position.latLng, {
|
|
221
|
-
label: text, // TODO: handle text rotation (leaflet-rotate)
|
|
222
|
-
radius: 7,
|
|
223
|
-
fillColor: '#fff',
|
|
224
|
-
fillOpacity: 1,
|
|
225
|
-
fillStyle: 'black',
|
|
226
|
-
color: '#777',
|
|
227
|
-
weight: 1,
|
|
228
|
-
interactive: false,
|
|
229
|
-
})
|
|
230
|
-
);
|
|
231
|
-
} else {
|
|
232
|
-
markers.addLayer(
|
|
233
|
-
L.marker(position.latLng, {
|
|
234
|
-
title: text,
|
|
235
|
-
icon: L.divIcon({
|
|
236
|
-
className: options.cssClass,
|
|
237
|
-
html: text,
|
|
238
|
-
iconSize: options.iconSize
|
|
239
|
-
}),
|
|
240
|
-
interactive: false,
|
|
241
|
-
})
|
|
242
|
-
);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
const updateMarkerVisibility = () => {
|
|
249
|
-
let oldZoom = this._lastZoomLevel || 0;
|
|
250
|
-
let newZoom = map.getZoom();
|
|
251
|
-
if (newZoom > oldZoom) {
|
|
252
|
-
for (let i = oldZoom + 1; i <= newZoom; ++i) {
|
|
253
|
-
if (this._zoomLayers[i] !== undefined) {
|
|
254
|
-
this.addLayer(this._zoomLayers[i]);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
} else if (newZoom < oldZoom) {
|
|
258
|
-
for (let i = oldZoom; i > newZoom; --i) {
|
|
259
|
-
if (this._zoomLayers[i] !== undefined) {
|
|
260
|
-
this.removeLayer(this._zoomLayers[i]);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
this._lastZoomLevel = newZoom;
|
|
265
|
-
};
|
|
266
|
-
map.on('zoomend', updateMarkerVisibility);
|
|
267
|
-
updateMarkerVisibility();
|
|
268
|
-
},
|
|
269
|
-
|
|
270
|
-
_minimumZoomLevelForItem: function (i, zoom) {
|
|
271
|
-
while (i > 0 && i % 2 === 0) {
|
|
272
|
-
--zoom;
|
|
273
|
-
i = Math.floor(i / 2);
|
|
274
|
-
}
|
|
275
|
-
return zoom;
|
|
276
|
-
},
|
|
277
|
-
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
L.Polyline.include({
|
|
281
|
-
|
|
282
|
-
_originalOnAdd: L.Polyline.prototype.onAdd,
|
|
283
|
-
_originalOnRemove: L.Polyline.prototype.onRemove,
|
|
284
|
-
|
|
285
|
-
addDistanceMarkers: function () {
|
|
286
|
-
if (this._map && this._distanceMarkers) {
|
|
287
|
-
this._map.addLayer(this._distanceMarkers);
|
|
288
|
-
}
|
|
289
|
-
},
|
|
290
|
-
|
|
291
|
-
removeDistanceMarkers: function () {
|
|
292
|
-
if (this._map && this._distanceMarkers) {
|
|
293
|
-
this._map.removeLayer(this._distanceMarkers);
|
|
294
|
-
}
|
|
295
|
-
},
|
|
296
|
-
|
|
297
|
-
onAdd: function (map) {
|
|
298
|
-
this._originalOnAdd(map);
|
|
299
|
-
|
|
300
|
-
let opts = this.options.distanceMarkers || {};
|
|
301
|
-
if (this.options.distanceMarkers) {
|
|
302
|
-
this._distanceMarkers = this._distanceMarkers || new L.DistanceMarkers(this, map, opts);
|
|
303
|
-
}
|
|
304
|
-
if (opts.lazy === undefined || opts.lazy === false) {
|
|
305
|
-
this.addDistanceMarkers();
|
|
306
|
-
}
|
|
307
|
-
},
|
|
308
|
-
|
|
309
|
-
onRemove: function (map) {
|
|
310
|
-
this.removeDistanceMarkers();
|
|
311
|
-
this._originalOnRemove(map);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
});
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2022, GPL-3.0+ Project, Raruto
|
|
3
|
+
*
|
|
4
|
+
* This file is free software: you may copy, redistribute and/or modify it
|
|
5
|
+
* under the terms of the GNU General Public License as published by the
|
|
6
|
+
* Free Software Foundation, either version 2 of the License, or (at your
|
|
7
|
+
* option) any later version.
|
|
8
|
+
*
|
|
9
|
+
* This file is distributed in the hope that it will be useful, but
|
|
10
|
+
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+
* General Public License for more details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU General Public License
|
|
15
|
+
* along with this program. If not, see .
|
|
16
|
+
*
|
|
17
|
+
* This file incorporates work covered by the following copyright and
|
|
18
|
+
* permission notice:
|
|
19
|
+
*
|
|
20
|
+
* Copyright (c) 2014- Doroszlai Attila, 2016- Phil Whitehurst
|
|
21
|
+
*
|
|
22
|
+
* Permission to use, copy, modify, and/or distribute this software
|
|
23
|
+
* for any purpose with or without fee is hereby granted, provided
|
|
24
|
+
* that the above copyright notice and this permission notice appear
|
|
25
|
+
* in all copies.
|
|
26
|
+
*
|
|
27
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
28
|
+
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
29
|
+
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
30
|
+
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
|
|
31
|
+
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
32
|
+
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
33
|
+
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
34
|
+
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
// TODO: the "L.DistanceMarker" (canvas marker) class could be alternatively provided by "leaflet-rotate"?
|
|
38
|
+
|
|
39
|
+
L.DistanceMarker = L.CircleMarker.extend({
|
|
40
|
+
_updatePath: function () {
|
|
41
|
+
let ctx = this._renderer._ctx;
|
|
42
|
+
let p = this._point;
|
|
43
|
+
|
|
44
|
+
// Calculate image direction (rotation)
|
|
45
|
+
this.options.rotation = this.options.rotation || 0;
|
|
46
|
+
|
|
47
|
+
// Draw circle marker (canvas point)
|
|
48
|
+
if (this.options.radius && this._renderer._updateCircle) {
|
|
49
|
+
this._renderer._updateCircle(this);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Draw image over circle (distance marker)
|
|
53
|
+
if (this.options.icon && this.options.icon.url) {
|
|
54
|
+
if (!this.options.icon.element) {
|
|
55
|
+
const icon = document.createElement('img');
|
|
56
|
+
this.options.icon = L.extend({ rotate: 0, size: [40, 40], offset: { x: 0, y: 0 } }, this.options.icon);
|
|
57
|
+
this.options.icon.rotate += this.options.rotation;
|
|
58
|
+
this.options.icon.element = icon;
|
|
59
|
+
icon.src = this.options.icon.url;
|
|
60
|
+
icon.onload = () => this.redraw();
|
|
61
|
+
icon.onerror = () => this.options.icon = null;
|
|
62
|
+
} else {
|
|
63
|
+
const icon = this.options.icon;
|
|
64
|
+
let cx = p.x + icon.offset.x;
|
|
65
|
+
let cy = p.y + icon.offset.y;
|
|
66
|
+
ctx.save();
|
|
67
|
+
if (icon.rotate) {
|
|
68
|
+
ctx.translate(p.x, p.y);
|
|
69
|
+
ctx.rotate(icon.rotate);
|
|
70
|
+
cx = 0;
|
|
71
|
+
cy = 0;
|
|
72
|
+
}
|
|
73
|
+
ctx.drawImage(icon.element, cx - icon.size[0] / 2, cy - icon.size[1] / 2, icon.size[0], icon.size[1]);
|
|
74
|
+
ctx.restore();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Add a label inside the circle (distance marker)
|
|
79
|
+
if (this.options.label) {
|
|
80
|
+
let cx = p.x, cy = p.y;
|
|
81
|
+
ctx.save();
|
|
82
|
+
|
|
83
|
+
ctx.font = this.options.font || 'normal 7pt "Helvetica Neue", Arial, Helvetica, sans-serif';
|
|
84
|
+
ctx.textAlign = "center";
|
|
85
|
+
ctx.textBaseline = "middle";
|
|
86
|
+
ctx.fillStyle = this.options.fillStyle || 'black';
|
|
87
|
+
|
|
88
|
+
// TODO rescale circle to fit text
|
|
89
|
+
// let fontSize = Number(/[0-9\.]+/.exec(ctx.font)[0]);
|
|
90
|
+
// let fontWidth = ctx.measureText(this.options.html).width;
|
|
91
|
+
|
|
92
|
+
if (this.options.rotation) {
|
|
93
|
+
ctx.translate(p.x, p.y);
|
|
94
|
+
ctx.rotate(this.options.rotation);
|
|
95
|
+
cx = 0;
|
|
96
|
+
cy = 0;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Temporary fix to prevent stroke blurs at higher zoom levels
|
|
100
|
+
if (this._map.getZoom() > 17) {
|
|
101
|
+
ctx.fillStyle = this.options.strokeStyle || 'black';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
ctx.fillText(this.options.label, cx, cy);
|
|
105
|
+
|
|
106
|
+
if (this.options.strokeStyle && this._map.getZoom() <= 17) {
|
|
107
|
+
ctx.strokeStyle = this.options.strokeStyle;
|
|
108
|
+
ctx.strokeText(this.options.label, cx, cy);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
ctx.restore();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
L.DistanceMarkers = L.LayerGroup.extend({
|
|
117
|
+
options: {
|
|
118
|
+
cssClass: 'dist-marker',
|
|
119
|
+
iconSize: [12, 12],
|
|
120
|
+
arrowSize: [10, 10],
|
|
121
|
+
arrowUrl: "data:image/svg+xml,%3Csvg transform='rotate(90)' xmlns='http://www.w3.org/2000/svg' width='560px' height='560px' viewBox='0 0 560 560'%3E%3Cpath stroke-width='35' fill='%23000' stroke='%23FFF' d='M280,40L522,525L280,420L38,525z'/%3E%3C/svg%3E",
|
|
122
|
+
offset: 1000,
|
|
123
|
+
showAll: 12,
|
|
124
|
+
textFunction: (distance, i, offset) => i,
|
|
125
|
+
distance: true,
|
|
126
|
+
direction: true,
|
|
127
|
+
},
|
|
128
|
+
initialize: function (line, map, options) {
|
|
129
|
+
|
|
130
|
+
this._layers = {};
|
|
131
|
+
this._zoomLayers = {};
|
|
132
|
+
|
|
133
|
+
options = L.setOptions(this, options);
|
|
134
|
+
|
|
135
|
+
let preferCanvas = map.options.preferCanvas;
|
|
136
|
+
let showAll = Math.min(map.getMaxZoom(), options.showAll);
|
|
137
|
+
|
|
138
|
+
// You should use "leaflet-rotate" to show rotated arrow markers (preferCanvas: false)
|
|
139
|
+
if (!preferCanvas && !map.options.rotate) {
|
|
140
|
+
console.warn('Missing dependency: "leaflet-rotate"');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Get line coords as an array
|
|
144
|
+
let coords = typeof line.getLatLngs == 'function' ? line.getLatLngs() : line;
|
|
145
|
+
|
|
146
|
+
// Handle "MultiLineString" features
|
|
147
|
+
coords = L.LineUtil.isFlat(coords) ? [coords] : coords;
|
|
148
|
+
|
|
149
|
+
coords.forEach(latlngs => {
|
|
150
|
+
// Get accumulated line lengths as well as overall length
|
|
151
|
+
let accumulated = L.GeometryUtil.accumulatedLengths(latlngs);
|
|
152
|
+
let length = accumulated.length > 0 ? accumulated[accumulated.length - 1] : 0;
|
|
153
|
+
|
|
154
|
+
// count = Number of distance markers to be added
|
|
155
|
+
// j = Position in accumulated line length array
|
|
156
|
+
for (let i = 1, count = Math.floor(length / options.offset), j = 0; i <= count; ++i) {
|
|
157
|
+
|
|
158
|
+
let distance = options.offset * i;
|
|
159
|
+
|
|
160
|
+
// Find the first accumulated distance that is greater than the distance of this marker
|
|
161
|
+
while (j < accumulated.length - 1 && accumulated[j] < distance) ++j;
|
|
162
|
+
|
|
163
|
+
// Grab two nearest points either side marker position
|
|
164
|
+
let p1 = latlngs[j - 1];
|
|
165
|
+
let p2 = latlngs[j];
|
|
166
|
+
let m_line = L.polyline([p1, p2]);
|
|
167
|
+
|
|
168
|
+
// and create a simple line to interpolate on
|
|
169
|
+
let ratio = (distance - accumulated[j - 1]) / (accumulated[j] - accumulated[j - 1]);
|
|
170
|
+
let position = L.GeometryUtil.interpolateOnLine(map, m_line, ratio);
|
|
171
|
+
let delta = map.project(p2).subtract(map.project(p1));
|
|
172
|
+
let angle = Math.atan2(delta.y, delta.x);
|
|
173
|
+
|
|
174
|
+
// Generate distance marker label
|
|
175
|
+
let text = options.textFunction.call(this, distance, i, options.offset);
|
|
176
|
+
|
|
177
|
+
// Grouping layer of visible layers at zoom level (arrow + distance)
|
|
178
|
+
let zoom = this._minimumZoomLevelForItem(i, showAll);
|
|
179
|
+
let markers = this._zoomLayers[zoom] = this._zoomLayers[zoom] || L.layerGroup()
|
|
180
|
+
|
|
181
|
+
// create arrow markers
|
|
182
|
+
if (options.direction && ((options.distance && i % 2 == 1) || !options.distance)) {
|
|
183
|
+
if (preferCanvas) {
|
|
184
|
+
markers.addLayer(
|
|
185
|
+
new L.DistanceMarker(p1, {
|
|
186
|
+
radius: 0,
|
|
187
|
+
icon: {
|
|
188
|
+
url: options.arrowUrl, //image link
|
|
189
|
+
size: options.arrowSize, //image size ( default [40, 40] )
|
|
190
|
+
rotate: 0, //image base rotate ( default 0 )
|
|
191
|
+
offset: { x: 0, y: 0 }, //image offset ( default { x: 0, y: 0 } )
|
|
192
|
+
},
|
|
193
|
+
rotation: angle,
|
|
194
|
+
interactive: false,
|
|
195
|
+
// label: '⮞', //'➜',
|
|
196
|
+
// font: 'normal 20pt "Helvetica Neue", Arial, Helvetica, sans-serif',
|
|
197
|
+
// fillStyle: 'white',//'#3366CC',
|
|
198
|
+
// strokeStyle: 'black',
|
|
199
|
+
})
|
|
200
|
+
);
|
|
201
|
+
} else {
|
|
202
|
+
markers.addLayer(
|
|
203
|
+
L.marker(position.latLng, {
|
|
204
|
+
icon: L.icon({
|
|
205
|
+
iconUrl: options.arrowUrl,
|
|
206
|
+
iconSize: options.arrowSize,
|
|
207
|
+
}),
|
|
208
|
+
// NB the following option is added by "leaflet-rotate"
|
|
209
|
+
rotation: angle * L.DomUtil.RAD_TO_DEG,
|
|
210
|
+
interactive: false,
|
|
211
|
+
})
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// create distance markers
|
|
217
|
+
if (options.distance && i % 2 == 0) {
|
|
218
|
+
if (preferCanvas) {
|
|
219
|
+
markers.addLayer(
|
|
220
|
+
new L.DistanceMarker(position.latLng, {
|
|
221
|
+
label: text, // TODO: handle text rotation (leaflet-rotate)
|
|
222
|
+
radius: 7,
|
|
223
|
+
fillColor: '#fff',
|
|
224
|
+
fillOpacity: 1,
|
|
225
|
+
fillStyle: 'black',
|
|
226
|
+
color: '#777',
|
|
227
|
+
weight: 1,
|
|
228
|
+
interactive: false,
|
|
229
|
+
})
|
|
230
|
+
);
|
|
231
|
+
} else {
|
|
232
|
+
markers.addLayer(
|
|
233
|
+
L.marker(position.latLng, {
|
|
234
|
+
title: text,
|
|
235
|
+
icon: L.divIcon({
|
|
236
|
+
className: options.cssClass,
|
|
237
|
+
html: text,
|
|
238
|
+
iconSize: options.iconSize
|
|
239
|
+
}),
|
|
240
|
+
interactive: false,
|
|
241
|
+
})
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
const updateMarkerVisibility = () => {
|
|
249
|
+
let oldZoom = this._lastZoomLevel || 0;
|
|
250
|
+
let newZoom = map.getZoom();
|
|
251
|
+
if (newZoom > oldZoom) {
|
|
252
|
+
for (let i = oldZoom + 1; i <= newZoom; ++i) {
|
|
253
|
+
if (this._zoomLayers[i] !== undefined) {
|
|
254
|
+
this.addLayer(this._zoomLayers[i]);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
} else if (newZoom < oldZoom) {
|
|
258
|
+
for (let i = oldZoom; i > newZoom; --i) {
|
|
259
|
+
if (this._zoomLayers[i] !== undefined) {
|
|
260
|
+
this.removeLayer(this._zoomLayers[i]);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
this._lastZoomLevel = newZoom;
|
|
265
|
+
};
|
|
266
|
+
map.on('zoomend', updateMarkerVisibility);
|
|
267
|
+
updateMarkerVisibility();
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
_minimumZoomLevelForItem: function (i, zoom) {
|
|
271
|
+
while (i > 0 && i % 2 === 0) {
|
|
272
|
+
--zoom;
|
|
273
|
+
i = Math.floor(i / 2);
|
|
274
|
+
}
|
|
275
|
+
return zoom;
|
|
276
|
+
},
|
|
277
|
+
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
L.Polyline.include({
|
|
281
|
+
|
|
282
|
+
_originalOnAdd: L.Polyline.prototype.onAdd,
|
|
283
|
+
_originalOnRemove: L.Polyline.prototype.onRemove,
|
|
284
|
+
|
|
285
|
+
addDistanceMarkers: function () {
|
|
286
|
+
if (this._map && this._distanceMarkers) {
|
|
287
|
+
this._map.addLayer(this._distanceMarkers);
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
|
|
291
|
+
removeDistanceMarkers: function () {
|
|
292
|
+
if (this._map && this._distanceMarkers) {
|
|
293
|
+
this._map.removeLayer(this._distanceMarkers);
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
|
|
297
|
+
onAdd: function (map) {
|
|
298
|
+
this._originalOnAdd(map);
|
|
299
|
+
|
|
300
|
+
let opts = this.options.distanceMarkers || {};
|
|
301
|
+
if (this.options.distanceMarkers) {
|
|
302
|
+
this._distanceMarkers = this._distanceMarkers || new L.DistanceMarkers(this, map, opts);
|
|
303
|
+
}
|
|
304
|
+
if (opts.lazy === undefined || opts.lazy === false) {
|
|
305
|
+
this.addDistanceMarkers();
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
|
|
309
|
+
onRemove: function (map) {
|
|
310
|
+
this.removeDistanceMarkers();
|
|
311
|
+
this._originalOnRemove(map);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
});
|