@raruto/leaflet-elevation 1.9.1 → 1.9.4

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.
@@ -1,109 +1,269 @@
1
1
  /*
2
- * https://github.com/adoroszlai/leaflet-distance-markers
2
+ * Copyright (c) 2022, GPL-3.0+ Project, Raruto
3
3
  *
4
- * The MIT License (MIT)
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.
5
8
  *
6
- * Copyright (c) 2014- Doroszlai Attila, 2016- Phil Whitehurst
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.
7
13
  *
8
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- * this software and associated documentation files (the "Software"), to deal in
10
- * the Software without restriction, including without limitation the rights to
11
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12
- * the Software, and to permit persons to whom the Software is furnished to do so,
13
- * subject to the following conditions:
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program. If not, see .
14
16
  *
15
- * The above copyright notice and this permission notice shall be included in all
16
- * copies or substantial portions of the Software.
17
+ * This file incorporates work covered by the following copyright and
18
+ * permission notice:
19
+ *
20
+ * Copyright (c) 2014- Doroszlai Attila, 2016- Phil Whitehurst
17
21
  *
18
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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.
24
35
  */
25
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
+
26
116
  L.DistanceMarkers = L.LayerGroup.extend({
117
+ __arrowIcon: "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='%23FFF' stroke='%23000' d='M280,40L522,525L280,420L38,525z'/%3E%3C/svg%3E",
118
+ options: {
119
+ cssClass: 'dist-marker',
120
+ iconSize: [12, 12],
121
+ offset: 1000,
122
+ showAll: 12,
123
+ textFunction: (distance, i, offset) => i,
124
+ },
27
125
  initialize: function (line, map, options) {
28
- options = options || {};
29
- var offset = options.offset || 1000;
30
- var showAll = Math.min(map.getMaxZoom(), options.showAll || 12);
31
- var cssClass = options.cssClass || 'dist-marker';
32
- var iconSize = options.iconSize !== undefined ? options.iconSize : [12, 12];
33
- var textFunction = options.textFunction || function(distance, i, offset) {
34
- return i;
35
- };
36
126
 
37
- var zoomLayers = {};
38
- // Get line coords as an array
39
- var coords = line;
40
- if (typeof line.getLatLngs == 'function') {
41
- coords = line.getLatLngs();
127
+ this._layers = {};
128
+ this._zoomLayers = {};
129
+
130
+ options = L.setOptions(this, options);
131
+
132
+ let preferCanvas = map.options.preferCanvas;
133
+ let showAll = Math.min(map.getMaxZoom(), options.showAll);
134
+
135
+ // You should use "leaflet-rotate" to show rotated arrow markers (preferCanvas: false)
136
+ if (!preferCanvas && !map.options.rotate) {
137
+ console.warn('Missing dependency: "leaflet-rotate"');
42
138
  }
139
+
140
+ // Get line coords as an array
141
+ let coords = typeof line.getLatLngs == 'function' ? line.getLatLngs() : line;
142
+
43
143
  // Get accumulated line lengths as well as overall length
44
- var accumulated = L.GeometryUtil.accumulatedLengths(line);
45
- var length = accumulated.length > 0 ? accumulated[accumulated.length - 1] : 0;
46
- // Position in accumulated line length array
47
- var j = 0;
48
- // Number of distance markers to be added
49
- var count = Math.floor(length / offset);
50
-
51
- for (var i = 1; i <= count; ++i) {
52
- var distance = offset * i;
53
- // Find the first accumulated distance that is greater
54
- // than the distance of this marker
55
- while (j < accumulated.length - 1 && accumulated[j] < distance) {
56
- ++j;
144
+ let accumulated = L.GeometryUtil.accumulatedLengths(line);
145
+ let length = accumulated.length > 0 ? accumulated[accumulated.length - 1] : 0;
146
+
147
+ // count = Number of distance markers to be added
148
+ // j = Position in accumulated line length array
149
+ for (let i = 1, count = Math.floor(length / options.offset), j = 0; i <= count; ++i) {
150
+
151
+ let distance = options.offset * i;
152
+
153
+ // Find the first accumulated distance that is greater than the distance of this marker
154
+ while (j < accumulated.length - 1 && accumulated[j] < distance) ++j;
155
+
156
+ // Grab two nearest points either side marker position
157
+ let p1 = coords[j - 1];
158
+ let p2 = coords[j];
159
+ let m_line = L.polyline([p1, p2]);
160
+
161
+ // and create a simple line to interpolate on
162
+ let ratio = (distance - accumulated[j - 1]) / (accumulated[j] - accumulated[j - 1]);
163
+ let position = L.GeometryUtil.interpolateOnLine(map, m_line, ratio);
164
+ let delta = map.project(p2).subtract(map.project(p1));
165
+ let angle = Math.atan2(delta.y, delta.x);
166
+
167
+ // Generate distance marker label
168
+ let text = options.textFunction.call(this, distance, i, options.offset);
169
+
170
+ // Grouping layer of generated markers (arrow + distance)
171
+ let marker = L.layerGroup();
172
+
173
+ // Grouping layer of visible layers (at zoom level)
174
+ let zoom = this._minimumZoomLevelForItem(i, showAll);
175
+ this._zoomLayers[zoom] = this._zoomLayers[zoom] || L.layerGroup()
176
+ this._zoomLayers[zoom].addLayer(marker);
177
+
178
+ // create arrow markers
179
+ if (options.arrows && ((options.distance && i % 2 == 1) || !options.distance)) {
180
+ if (preferCanvas) {
181
+ marker.addLayer(
182
+ new L.DistanceMarker(p1, {
183
+ radius: 0,
184
+ icon: {
185
+ url: this.__arrowIcon, //image link
186
+ size: [20, 20], //image size ( default [40, 40] )
187
+ rotate: 0, //image base rotate ( default 0 )
188
+ offset: { x: 0, y: 0 },//image offset ( default { x: 0, y: 0 } )
189
+ },
190
+ rotation: angle,
191
+ interactive: false,
192
+ // label: '⮞', //'➜',
193
+ // font: 'normal 20pt "Helvetica Neue", Arial, Helvetica, sans-serif',
194
+ // fillStyle: 'white',//'#3366CC',
195
+ // strokeStyle: 'black',
196
+ })
197
+ );
198
+ } else {
199
+ marker.addLayer(
200
+ L.marker(position.latLng, {
201
+ icon: L.icon({
202
+ iconUrl: __arrowIcon,
203
+ iconSize: [20, 20],
204
+ }),
205
+ // NB the following option is added by "leaflet-rotate"
206
+ rotation: angle * L.DomUtil.RAD_TO_DEG,
207
+ interactive: false,
208
+ })
209
+ );
210
+ }
57
211
  }
58
- // Now grab the two nearest points either side of
59
- // distance marker position and create a simple line to
60
- // interpolate on
61
- var p1 = coords[j - 1];
62
- var p2 = coords[j];
63
- var m_line = L.polyline([p1, p2]);
64
- var ratio = (distance - accumulated[j - 1]) / (accumulated[j] - accumulated[j - 1]);
65
- var position = L.GeometryUtil.interpolateOnLine(map, m_line, ratio);
66
- var text = textFunction.call(this, distance, i, offset);
67
- var icon = L.divIcon({ className: cssClass, html: text, iconSize: iconSize });
68
- var marker = L.marker(position.latLng, { title: text, icon: icon });
69
-
70
- // visible only starting at a specific zoom level
71
- var zoom = this._minimumZoomLevelForItem(i, showAll);
72
- if (zoomLayers[zoom] === undefined) {
73
- zoomLayers[zoom] = L.layerGroup();
212
+
213
+ // create distance markers
214
+ if (options.distance && i % 2 == 0) {
215
+ if (preferCanvas) {
216
+ marker.addLayer(
217
+ new L.DistanceMarker(position.latLng, {
218
+ label: text, // TODO: handle text rotation (leaflet-rotate)
219
+ radius: 7,
220
+ fillColor: '#fff',
221
+ fillOpacity: 1,
222
+ fillStyle: 'black',
223
+ color: '#777',
224
+ weight: 1,
225
+ interactive: false,
226
+ })
227
+ );
228
+ } else {
229
+ marker.addLayer(
230
+ L.marker(position.latLng, {
231
+ title: text,
232
+ icon: L.divIcon({
233
+ className: options.cssClass,
234
+ html: text,
235
+ iconSize: options.iconSize
236
+ }),
237
+ interactive: false,
238
+ })
239
+ );
240
+ }
74
241
  }
75
- zoomLayers[zoom].addLayer(marker);
76
242
  }
77
243
 
78
- var currentZoomLevel = 0;
79
- var markerLayer = this;
80
- var updateMarkerVisibility = function() {
81
- var oldZoom = currentZoomLevel;
82
- var newZoom = currentZoomLevel = map.getZoom();
83
-
244
+ const updateMarkerVisibility = () => {
245
+ let oldZoom = this._lastZoomLevel || 0;
246
+ let newZoom = map.getZoom();
84
247
  if (newZoom > oldZoom) {
85
- for (var i = oldZoom + 1; i <= newZoom; ++i) {
86
- if (zoomLayers[i] !== undefined) {
87
- markerLayer.addLayer(zoomLayers[i]);
248
+ for (let i = oldZoom + 1; i <= newZoom; ++i) {
249
+ if (this._zoomLayers[i] !== undefined) {
250
+ this.addLayer(this._zoomLayers[i]);
88
251
  }
89
252
  }
90
253
  } else if (newZoom < oldZoom) {
91
- for (var i = oldZoom; i > newZoom; --i) {
92
- if (zoomLayers[i] !== undefined) {
93
- markerLayer.removeLayer(zoomLayers[i]);
254
+ for (let i = oldZoom; i > newZoom; --i) {
255
+ if (this._zoomLayers[i] !== undefined) {
256
+ this.removeLayer(this._zoomLayers[i]);
94
257
  }
95
258
  }
96
259
  }
260
+ this._lastZoomLevel = newZoom;
97
261
  };
98
262
  map.on('zoomend', updateMarkerVisibility);
99
-
100
- this._layers = {}; // need to initialize before adding markers to this LayerGroup
101
263
  updateMarkerVisibility();
102
264
  },
103
265
 
104
- _minimumZoomLevelForItem: function (item, showAllLevel) {
105
- var zoom = showAllLevel;
106
- var i = item;
266
+ _minimumZoomLevelForItem: function (i, zoom) {
107
267
  while (i > 0 && i % 2 === 0) {
108
268
  --zoom;
109
269
  i = Math.floor(i / 2);
@@ -133,9 +293,9 @@ L.Polyline.include({
133
293
  onAdd: function (map) {
134
294
  this._originalOnAdd(map);
135
295
 
136
- var opts = this.options.distanceMarkers || {};
137
- if (this._distanceMarkers === undefined && this.options.distanceMarkers) {
138
- this._distanceMarkers = new L.DistanceMarkers(this, map, opts);
296
+ let opts = this.options.distanceMarkers || {};
297
+ if (this.options.distanceMarkers) {
298
+ this._distanceMarkers = this._distanceMarkers || new L.DistanceMarkers(this, map, opts);
139
299
  }
140
300
  if (opts.lazy === undefined || opts.lazy === false) {
141
301
  this.addDistanceMarkers();
@@ -1 +1 @@
1
- L.DistanceMarkers=L.LayerGroup.extend({initialize:function(line,map,options){var offset=(options=options||{}).offset||1e3,showAll=Math.min(map.getMaxZoom(),options.showAll||12),cssClass=options.cssClass||"dist-marker",iconSize=void 0!==options.iconSize?options.iconSize:[12,12],textFunction=options.textFunction||function(distance,i,offset){return i},zoomLayers={},coords=line;"function"==typeof line.getLatLngs&&(coords=line.getLatLngs());for(var accumulated=L.GeometryUtil.accumulatedLengths(line),length=accumulated.length>0?accumulated[accumulated.length-1]:0,j=0,count=Math.floor(length/offset),i=1;i<=count;++i){for(var distance=offset*i;j<accumulated.length-1&&accumulated[j]<distance;)++j;var p1=coords[j-1],p2=coords[j],m_line=L.polyline([p1,p2]),ratio=(distance-accumulated[j-1])/(accumulated[j]-accumulated[j-1]),position=L.GeometryUtil.interpolateOnLine(map,m_line,ratio),text=textFunction.call(this,distance,i,offset),icon=L.divIcon({className:cssClass,html:text,iconSize:iconSize}),marker=L.marker(position.latLng,{title:text,icon:icon}),zoom=this._minimumZoomLevelForItem(i,showAll);void 0===zoomLayers[zoom]&&(zoomLayers[zoom]=L.layerGroup()),zoomLayers[zoom].addLayer(marker)}var currentZoomLevel=0,markerLayer=this,updateMarkerVisibility=function(){var oldZoom=currentZoomLevel,newZoom=currentZoomLevel=map.getZoom();if(newZoom>oldZoom)for(var i=oldZoom+1;i<=newZoom;++i)void 0!==zoomLayers[i]&&markerLayer.addLayer(zoomLayers[i]);else if(newZoom<oldZoom)for(var i=oldZoom;i>newZoom;--i)void 0!==zoomLayers[i]&&markerLayer.removeLayer(zoomLayers[i])};map.on("zoomend",updateMarkerVisibility),this._layers={},updateMarkerVisibility()},_minimumZoomLevelForItem:function(item,showAllLevel){for(var zoom=showAllLevel,i=item;i>0&&i%2==0;)--zoom,i=Math.floor(i/2);return zoom}}),L.Polyline.include({_originalOnAdd:L.Polyline.prototype.onAdd,_originalOnRemove:L.Polyline.prototype.onRemove,addDistanceMarkers:function(){this._map&&this._distanceMarkers&&this._map.addLayer(this._distanceMarkers)},removeDistanceMarkers:function(){this._map&&this._distanceMarkers&&this._map.removeLayer(this._distanceMarkers)},onAdd:function(map){this._originalOnAdd(map);var opts=this.options.distanceMarkers||{};void 0===this._distanceMarkers&&this.options.distanceMarkers&&(this._distanceMarkers=new L.DistanceMarkers(this,map,opts)),void 0!==opts.lazy&&!1!==opts.lazy||this.addDistanceMarkers()},onRemove:function(map){this.removeDistanceMarkers(),this._originalOnRemove(map)}});
1
+ L.DistanceMarker=L.CircleMarker.extend({_updatePath:function(){let ctx=this._renderer._ctx,p=this._point;if(this.options.rotation=this.options.rotation||0,this.options.radius&&this._renderer._updateCircle&&this._renderer._updateCircle(this),this.options.icon&&this.options.icon.url)if(this.options.icon.element){const icon=this.options.icon;let cx=p.x+icon.offset.x,cy=p.y+icon.offset.y;ctx.save(),icon.rotate&&(ctx.translate(p.x,p.y),ctx.rotate(icon.rotate),cx=0,cy=0),ctx.drawImage(icon.element,cx-icon.size[0]/2,cy-icon.size[1]/2,icon.size[0],icon.size[1]),ctx.restore()}else{const icon=document.createElement("img");this.options.icon=L.extend({rotate:0,size:[40,40],offset:{x:0,y:0}},this.options.icon),this.options.icon.rotate+=this.options.rotation,this.options.icon.element=icon,icon.src=this.options.icon.url,icon.onload=()=>this.redraw(),icon.onerror=()=>this.options.icon=null}if(this.options.label){let cx=p.x,cy=p.y;ctx.save(),ctx.font=this.options.font||'normal 7pt "Helvetica Neue", Arial, Helvetica, sans-serif',ctx.textAlign="center",ctx.textBaseline="middle",ctx.fillStyle=this.options.fillStyle||"black",this.options.rotation&&(ctx.translate(p.x,p.y),ctx.rotate(this.options.rotation),cx=0,cy=0),this._map.getZoom()>17&&(ctx.fillStyle=this.options.strokeStyle||"black"),ctx.fillText(this.options.label,cx,cy),this.options.strokeStyle&&this._map.getZoom()<=17&&(ctx.strokeStyle=this.options.strokeStyle,ctx.strokeText(this.options.label,cx,cy)),ctx.restore()}}}),L.DistanceMarkers=L.LayerGroup.extend({__arrowIcon:"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='%23FFF' stroke='%23000' d='M280,40L522,525L280,420L38,525z'/%3E%3C/svg%3E",options:{cssClass:"dist-marker",iconSize:[12,12],offset:1e3,showAll:12,textFunction:(distance,i,offset)=>i},initialize:function(line,map,options){this._layers={},this._zoomLayers={},options=L.setOptions(this,options);let preferCanvas=map.options.preferCanvas,showAll=Math.min(map.getMaxZoom(),options.showAll);preferCanvas||map.options.rotate||console.warn('Missing dependency: "leaflet-rotate"');let coords="function"==typeof line.getLatLngs?line.getLatLngs():line,accumulated=L.GeometryUtil.accumulatedLengths(line),length=accumulated.length>0?accumulated[accumulated.length-1]:0;for(let i=1,count=Math.floor(length/options.offset),j=0;i<=count;++i){let distance=options.offset*i;for(;j<accumulated.length-1&&accumulated[j]<distance;)++j;let p1=coords[j-1],p2=coords[j],m_line=L.polyline([p1,p2]),ratio=(distance-accumulated[j-1])/(accumulated[j]-accumulated[j-1]),position=L.GeometryUtil.interpolateOnLine(map,m_line,ratio),delta=map.project(p2).subtract(map.project(p1)),angle=Math.atan2(delta.y,delta.x),text=options.textFunction.call(this,distance,i,options.offset),marker=L.layerGroup(),zoom=this._minimumZoomLevelForItem(i,showAll);this._zoomLayers[zoom]=this._zoomLayers[zoom]||L.layerGroup(),this._zoomLayers[zoom].addLayer(marker),options.arrows&&(options.distance&&i%2==1||!options.distance)&&(preferCanvas?marker.addLayer(new L.DistanceMarker(p1,{radius:0,icon:{url:this.__arrowIcon,size:[20,20],rotate:0,offset:{x:0,y:0}},rotation:angle,interactive:!1})):marker.addLayer(L.marker(position.latLng,{icon:L.icon({iconUrl:__arrowIcon,iconSize:[20,20]}),rotation:angle*L.DomUtil.RAD_TO_DEG,interactive:!1}))),options.distance&&i%2==0&&(preferCanvas?marker.addLayer(new L.DistanceMarker(position.latLng,{label:text,radius:7,fillColor:"#fff",fillOpacity:1,fillStyle:"black",color:"#777",weight:1,interactive:!1})):marker.addLayer(L.marker(position.latLng,{title:text,icon:L.divIcon({className:options.cssClass,html:text,iconSize:options.iconSize}),interactive:!1})))}const updateMarkerVisibility=()=>{let oldZoom=this._lastZoomLevel||0,newZoom=map.getZoom();if(newZoom>oldZoom)for(let i=oldZoom+1;i<=newZoom;++i)void 0!==this._zoomLayers[i]&&this.addLayer(this._zoomLayers[i]);else if(newZoom<oldZoom)for(let i=oldZoom;i>newZoom;--i)void 0!==this._zoomLayers[i]&&this.removeLayer(this._zoomLayers[i]);this._lastZoomLevel=newZoom};map.on("zoomend",updateMarkerVisibility),updateMarkerVisibility()},_minimumZoomLevelForItem:function(i,zoom){for(;i>0&&i%2==0;)--zoom,i=Math.floor(i/2);return zoom}}),L.Polyline.include({_originalOnAdd:L.Polyline.prototype.onAdd,_originalOnRemove:L.Polyline.prototype.onRemove,addDistanceMarkers:function(){this._map&&this._distanceMarkers&&this._map.addLayer(this._distanceMarkers)},removeDistanceMarkers:function(){this._map&&this._distanceMarkers&&this._map.removeLayer(this._distanceMarkers)},onAdd:function(map){this._originalOnAdd(map);let opts=this.options.distanceMarkers||{};this.options.distanceMarkers&&(this._distanceMarkers=this._distanceMarkers||new L.DistanceMarkers(this,map,opts)),void 0!==opts.lazy&&!1!==opts.lazy||this.addDistanceMarkers()},onRemove:function(map){this.removeDistanceMarkers(),this._originalOnRemove(map)}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raruto/leaflet-elevation",
3
- "version": "1.9.1",
3
+ "version": "1.9.4",
4
4
  "description": "A Leaflet plugin that allows to add elevation profiles using d3js",
5
5
  "main": "dist/leaflet-elevation.min.js",
6
6
  "module": "src/index.js",
package/src/components.js CHANGED
@@ -116,9 +116,11 @@ export const PositionMarker = ({
116
116
  }) => {
117
117
  return g => {
118
118
 
119
- let line = g.select('.height-focus.line');
119
+ g.attr("class", "height-focus-group");
120
+
121
+ let line = g.select('.height-focus.line');
120
122
  let circle = g.select('.height-focus.circle-lower');
121
- let text = g.select('.height-focus-label');
123
+ let text = g.select('.height-focus-label');
122
124
 
123
125
  if (!line.node()) line = g.append('svg:line');
124
126
  if (!circle.node()) circle = g.append('svg:circle');
package/src/control.js CHANGED
@@ -17,7 +17,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
17
17
  __TOGEOJSON: 'https://unpkg.com/@tmcw/togeojson@4.5.0/dist/togeojson.umd.js',
18
18
  __LGEOMUTIL: 'https://unpkg.com/leaflet-geometryutil@0.9.3/src/leaflet.geometryutil.js',
19
19
  __LALMOSTOVER: 'https://unpkg.com/leaflet-almostover@1.0.1/src/leaflet.almostover.js',
20
- __LDISTANCEM: 'https://unpkg.com/@raruto/leaflet-elevation@1.9.1/libs/leaflet-distance-marker.min.js',
20
+ __LDISTANCEM: 'https://unpkg.com/@raruto/leaflet-elevation@1.9.4/libs/leaflet-distance-marker.min.js',
21
21
 
22
22
  /*
23
23
  * Add data to the diagram either from GPX or GeoJSON and update the axis domain and data
@@ -163,7 +163,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
163
163
  if (this.options.legend) this.options.margins.bottom += 30;
164
164
  if (this.options.theme) this.options.polylineSegments.className += ' ' + this.options.theme;
165
165
  if (this.options.wptIcons === true) this.options.wptIcons = Options.wptIcons;
166
- if (this.options.distanceMarkers === true) this.options.distanceMarkers = { lazy: true };
166
+ if (this.options.distanceMarkers === true) this.options.distanceMarkers = Options.distanceMarkers;
167
167
 
168
168
  this._markedSegments.setStyle(this.options.polylineSegments);
169
169
 
@@ -400,17 +400,6 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
400
400
  if (marker) this._markers.addLayer(marker)
401
401
  },
402
402
 
403
- _bindPopup: function(marker) {
404
- let popup = marker._popup;
405
- if (popup) {
406
- popup.options.className = 'elevation-popup';
407
- if (popup._content) {
408
- popup._content = decodeURI(popup._content);
409
- popup.bindTooltip(popup._content, { direction: 'auto', sticky: true, opacity: 1, className: 'elevation-tooltip' }).openTooltip();
410
- }
411
- }
412
- },
413
-
414
403
  /**
415
404
  * Initialize "L.AlmostOver" integration
416
405
  */
@@ -754,10 +743,11 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
754
743
  }
755
744
  let marker = L.marker(latlng, { icon: wptIcons[sym] });
756
745
  if ([true, 'markers'].includes(wptLabels) && (name || desc)) {
757
- marker.bindPopup("<b>" + name + "</b>" + (desc.length > 0 ? '<br>' + desc : '')).openPopup();
746
+ let content = decodeURI("<b>" + name + "</b>" + (desc.length > 0 ? '<br>' + desc : ''));
747
+ marker.bindPopup(content, { className: 'elevation-popup', keepInView: true }).openPopup();
748
+ marker.bindTooltip(content, { className: 'elevation-tooltip', direction: 'auto', sticky: true, opacity: 1 }).openTooltip();
758
749
  }
759
750
  control._addMarker(marker);
760
- control._bindPopup(marker);
761
751
  control.fire('waypoint_added', { point: marker, element: latlng, properties: prop });
762
752
  return marker;
763
753
  }
package/src/marker.js CHANGED
@@ -10,8 +10,6 @@ export var Marker = L.Class.extend({
10
10
  switch(this.options.marker) {
11
11
  case 'elevation-line':
12
12
  // this._container = d3.create("g").attr("class", "height-focus-group");
13
- this._container = d3.select(map.getPane('elevationPane')).select("svg > g")
14
- .attr("class", "height-focus-group");
15
13
  break;
16
14
  case 'position-marker':
17
15
  // this._marker = L.circleMarker([0, 0], { pane: 'overlayPane', radius: 6, fillColor: '#fff', fillOpacity:1, color: '#000', weight:1, interactive: false });
@@ -27,7 +25,7 @@ export var Marker = L.Class.extend({
27
25
  addTo: function(map) {
28
26
  this._map = map;
29
27
  switch(this.options.marker) {
30
- case 'elevation-line': this._container.call(D3.PositionMarker({})); break;
28
+ case 'elevation-line': this._container = d3.select(map.getPane('elevationPane')).select("svg > g").call(D3.PositionMarker({})); break;
31
29
  case 'position-marker': this._marker.addTo(map, { pane: 'overlayPane' }); break;
32
30
  }
33
31
  return this;
@@ -49,18 +47,20 @@ export var Marker = L.Class.extend({
49
47
 
50
48
  switch(this.options.marker) {
51
49
  case 'elevation-line':
52
- let point = this._map.latLngToLayerPoint(this._latlng);
53
- point = L.extend({}, props.item, this._map._rotate ? this._map.rotatedPointToMapPanePoint(point) : point);
54
-
55
- this._container.classed("leaflet-hidden", false);
56
- this._container.call(D3.PositionMarker({
57
- theme : this.options.theme,
58
- xCoord: point.x,
59
- yCoord: point.y,
60
- length: point.y - (this._height() / props.maxElevation * point.z), // normalized Y
61
- labels: this._labels,
62
- item: point
63
- }));
50
+ if (this._container) {
51
+ let point = this._map.latLngToLayerPoint(this._latlng);
52
+ point = L.extend({}, props.item, this._map._rotate ? this._map.rotatedPointToMapPanePoint(point) : point);
53
+
54
+ this._container.classed("leaflet-hidden", false);
55
+ this._container.call(D3.PositionMarker({
56
+ theme : this.options.theme,
57
+ xCoord: point.x,
58
+ yCoord: point.y,
59
+ length: point.y - (this._height() / props.maxElevation * point.z), // normalized Y
60
+ labels: this._labels,
61
+ item: point
62
+ }));
63
+ }
64
64
  break;
65
65
  case 'position-marker':
66
66
  _.removeClass(this._marker.getElement(), 'leaflet-hidden');
package/src/options.js CHANGED
@@ -9,7 +9,11 @@ export var Options = {
9
9
  collapsed: false,
10
10
  detached: true,
11
11
  distance: true,
12
- distanceMarkers: false,
12
+ distanceMarkers: {
13
+ lazy: true,
14
+ distance: true,
15
+ arrow: false
16
+ },
13
17
  decimalsX: 2,
14
18
  decimalsY: 0,
15
19
  dragging: !L.Browser.mobile,
@@ -69,7 +73,7 @@ export var Options = {
69
73
  wptIcons: {
70
74
  '': L.divIcon({
71
75
  className: 'elevation-waypoint-marker',
72
- html: '<i class="elevation-waypoint-icon"></i>',
76
+ html: '<i class="elevation-waypoint-icon default"></i>',
73
77
  iconSize: [30, 30],
74
78
  iconAnchor: [8, 30],
75
79
  }),