@raruto/leaflet-elevation 1.7.0 → 1.7.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.
@@ -100,11 +100,10 @@ L.control.layersLegend = function(baseLayers, overlays, options) {
100
100
  return new L.Control.LayersLegend(baseLayers, overlays, options);
101
101
  };
102
102
 
103
- L.GPX.include(L.Mixin.Selectable);
103
+ L.GeoJSON.include(L.Mixin.Selectable);
104
104
 
105
105
  L.GpxGroup = L.Class.extend({
106
106
  options: {
107
- async: true,
108
107
  highlight: {
109
108
  color: '#ff0',
110
109
  opacity: 1,
@@ -125,7 +124,6 @@ L.GpxGroup = L.Class.extend({
125
124
  elevation: true,
126
125
  elevation_options: {
127
126
  theme: 'yellow-theme',
128
- // width: 500,
129
127
  detachedView: true,
130
128
  elevationDiv: '#elevation-div',
131
129
  },
@@ -133,11 +131,6 @@ L.GpxGroup = L.Class.extend({
133
131
  distanceMarkers_options: {
134
132
  lazy: true
135
133
  },
136
- marker_options: {
137
- startIconUrl: null,
138
- endIconUrl: null,
139
- shadowUrl: null,
140
- }
141
134
  },
142
135
 
143
136
  initialize: function(tracks, options) {
@@ -187,10 +180,40 @@ L.GpxGroup = L.Class.extend({
187
180
  },
188
181
 
189
182
  addTrack: function(track) {
190
- this._get(track, this._loadRoute.bind(this));
183
+ fetch(track).then(response => {
184
+ if (response.ok) {
185
+ response.text().then(doc => {
186
+ let geojson;
187
+ doc = doc.trim();
188
+
189
+ try {
190
+ let xml = (new DOMParser()).parseFromString(doc, "text/xml");
191
+ let type = xml.documentElement.tagName.toLowerCase(); // "kml" or "gpx"
192
+ let name = xml.getElementsByTagName('name');
193
+ if (!(type in toGeoJSON)) {
194
+ type = xml.documentElement.tagName == "TrainingCenterDatabase" ? 'tcx' : 'gpx';
195
+ }
196
+ geojson = toGeoJSON[type](xml);
197
+ geojson.name = name.length > 0 ? Array.from(name).find(tag => tag.parentElement.tagName == "trk").textContent : '';
198
+ } catch (e) {
199
+ try {
200
+ geojson = JSON.parse(doc.toString());
201
+ } catch (e) {
202
+ console.warn("Error parsing track: " + track);
203
+ }
204
+ }
205
+ if (!geojson.name) {
206
+ geojson.name = track.split('/').pop().split('#')[0].split('?')[0];
207
+ }
208
+ this._loadRoute(geojson);
209
+ });
210
+ }
211
+ });
191
212
  },
192
213
 
193
214
  _loadRoute: function(data) {
215
+ if (!data) return;
216
+
194
217
  var colors = this._uniqueColors(this._tracks.length);
195
218
  var color = colors[this._count++];
196
219
 
@@ -201,23 +224,24 @@ L.GpxGroup = L.Class.extend({
201
224
  distanceMarkers: this.options.distanceMarkers_options,
202
225
  };
203
226
 
204
- var route = new L.GPX(data, {
205
- async: this.options.async,
206
- marker_options: this.options.marker_options,
207
- polyline_options: line_style
227
+ var route = L.geoJson(data, {
228
+ name: data.name || '',
229
+ style: (feature) => line_style,
230
+ distanceMarkers: line_style.distanceMarkers,
231
+ originalStyle: line_style,
232
+ filter: feature => feature.geometry.type != "Point",
208
233
  });
209
234
 
210
- route.originalStyle = line_style;
211
-
212
- route.on('addline', L.bind(this._onRouteAddLine, this, route));
213
- route.on('loaded', L.bind(this._onRouteLoaded, this, route));
214
-
215
235
  route.addTo(this._layers);
216
236
  this._routes[route._leaflet_id] = route;
237
+
238
+ route.eachLayer((layer) => this._onRouteLayer(route, layer));
239
+ this._onRouteLoaded(route);
240
+
217
241
  },
218
242
 
219
- _onRouteAddLine: function(route, e) {
220
- var polyline = e.line;
243
+ _onRouteLayer: function(route, layer) {
244
+ var polyline = layer;
221
245
 
222
246
  route.on('selected', L.bind(this._onRouteSelected, this, route, polyline));
223
247
 
@@ -225,7 +249,7 @@ L.GpxGroup = L.Class.extend({
225
249
  polyline.on('mouseout', L.bind(this._onRouteMouseOut, this, route, polyline));
226
250
  polyline.on('click', L.bind(this._onRouteClick, this, route, polyline));
227
251
 
228
- polyline.bindTooltip(route.get_name(), {
252
+ polyline.bindTooltip(route.options.name, {
229
253
  direction: 'auto',
230
254
  sticky: true,
231
255
  });
@@ -239,7 +263,7 @@ L.GpxGroup = L.Class.extend({
239
263
  },
240
264
 
241
265
  unhighlight: function(route, polyline) {
242
- polyline.setStyle(route.originalStyle);
266
+ polyline.setStyle(route.options.originalStyle);
243
267
  if (this.options.distanceMarkers) {
244
268
  polyline.removeDistanceMarkers();
245
269
  }
@@ -281,7 +305,7 @@ L.GpxGroup = L.Class.extend({
281
305
 
282
306
  _onRouteLoaded: function(route) {
283
307
  if (this.options.legend) {
284
- this._legend.addBaseLayer(route, '<svg id="legend_' + route._leaflet_id + '" width="25" height="10" version="1.1" xmlns="http://www.w3.org/2000/svg">' + '<line x1="0" x2="50" y1="5" y2="5" stroke="' + route.options.polyline_options.color + '" fill="transparent" stroke-width="5" /></svg>' + ' ' + route.get_name());
308
+ this._legend.addBaseLayer(route, '<svg id="legend_' + route._leaflet_id + '" width="25" height="10" version="1.1" xmlns="http://www.w3.org/2000/svg">' + '<line x1="0" x2="50" y1="5" y2="5" stroke="' + route.options.originalStyle.color + '" fill="transparent" stroke-width="5" /></svg>' + ' ' + route.options.name);
285
309
  }
286
310
  this.fire('route_loaded', {
287
311
  route: route,
@@ -314,7 +338,7 @@ L.GpxGroup = L.Class.extend({
314
338
  }
315
339
  route.getLayers().forEach(function(layer) {
316
340
  if (layer instanceof L.Polyline) {
317
- elevation.addData(layer);
341
+ elevation.addData(layer, false);
318
342
  layer.bringToFront();
319
343
  }
320
344
  });
@@ -353,17 +377,6 @@ L.GpxGroup = L.Class.extend({
353
377
  }
354
378
  },
355
379
 
356
- _get: function(url, callback) {
357
- var xhr = new XMLHttpRequest();
358
- xhr.open("GET", url);
359
- xhr.onreadystatechange = function() {
360
- if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
361
- callback(xhr.response);
362
- }
363
- };
364
- xhr.send();
365
- },
366
-
367
380
  _uniqueColors: function(count) {
368
381
  if (count === 0) return [];
369
382
  if (count === 1) return ['#0000ff'];
@@ -1,16 +1,17 @@
1
1
  'use strict';L.Mixin.Selectable={includes:L.Mixin.Events,setSelected:function(s){var selected=!!s;if(this._selected!==selected){this._selected=selected;this.fire("selected")}},isSelected:function(){return!!this._selected}};
2
2
  L.Mixin.Selection={includes:L.Mixin.Events,getSelection:function(){return this._selected},setSelection:function(item){if(this._selected===item){if(item!==null){item.setSelected(!item.isSelected());if(!item.isSelected())this._selected=null}}else{if(this._selected)this._selected.setSelected(false);this._selected=item;if(this._selected)this._selected.setSelected(true)}this.fire("selection_changed")}};
3
3
  L.Control.LayersLegend=L.Control.Layers.extend({_onInputClick:function(){var inputs=this._layerControlInputs,input,layer;var addedLayers=[],removedLayers=[];this._handlingClick=true;for(var i=inputs.length-1;i>=0;i--){input=inputs[i];layer=this._getLayer(input.layerId).layer;if(input.checked)this._map.fireEvent("legend_selected",{layer:layer,input:input},true)}this._handlingClick=false;this._refocusOnMap()}});
4
- L.control.layersLegend=function(baseLayers,overlays,options){return new L.Control.LayersLegend(baseLayers,overlays,options)};L.GPX.include(L.Mixin.Selectable);
5
- L.GpxGroup=L.Class.extend({options:{async:true,highlight:{color:"#ff0",opacity:1},points:[],points_options:{icon:{iconUrl:"../images/elevation-poi.png",iconSize:[12,12]}},flyToBounds:true,legend:false,legend_options:{position:"topright",collapsed:false},elevation:true,elevation_options:{theme:"yellow-theme",detachedView:true,elevationDiv:"#elevation-div"},distanceMarkers:true,distanceMarkers_options:{lazy:true},marker_options:{startIconUrl:null,endIconUrl:null,shadowUrl:null}},initialize:function(tracks,
6
- options){L.Util.setOptions(this,options);this._count=0;this._loadedCount=0;this._tracks=tracks;this._routes={};this._layers=L.featureGroup();this._markers=L.featureGroup();this._elevation=L.control.elevation(this.options.elevation_options);this._legend=L.control.layersLegend(null,null,this.options.legend_options);var icon=L.icon(this.options.points_options.icon);this.options.points.forEach(function(poi){var marker=L.marker(poi.latlng,{icon:icon});marker.bindTooltip(poi.name,{direction:"auto"});marker.addTo(this._markers)},
7
- this);if(typeof L.DistanceMarkers==="undefined")this.options.distanceMarkers=false},getBounds:function(){return this._layers.getBounds()},addTo:function(map){this._layers.addTo(map);this._markers.addTo(map);this._map=map;this.on("selection_changed",this._onSelectionChanged,this);this._map.on("legend_selected",this._onLegendSelected,this);this._tracks.forEach(this.addTrack,this)},addTrack:function(track){this._get(track,this._loadRoute.bind(this))},_loadRoute:function(data){var colors=this._uniqueColors(this._tracks.length);
8
- var color=colors[this._count++];var line_style={color:color,opacity:.75,weight:5,distanceMarkers:this.options.distanceMarkers_options};var route=new L.GPX(data,{async:this.options.async,marker_options:this.options.marker_options,polyline_options:line_style});route.originalStyle=line_style;route.on("addline",L.bind(this._onRouteAddLine,this,route));route.on("loaded",L.bind(this._onRouteLoaded,this,route));route.addTo(this._layers);this._routes[route._leaflet_id]=route},_onRouteAddLine:function(route,
9
- e){var polyline=e.line;route.on("selected",L.bind(this._onRouteSelected,this,route,polyline));polyline.on("mouseover",L.bind(this._onRouteMouseOver,this,route,polyline));polyline.on("mouseout",L.bind(this._onRouteMouseOut,this,route,polyline));polyline.on("click",L.bind(this._onRouteClick,this,route,polyline));polyline.bindTooltip(route.get_name(),{direction:"auto",sticky:true})},highlight:function(route,polyline){polyline.setStyle(this.options.highlight);if(this.options.distanceMarkers)polyline.addDistanceMarkers()},
10
- unhighlight:function(route,polyline){polyline.setStyle(route.originalStyle);if(this.options.distanceMarkers)polyline.removeDistanceMarkers()},_onRouteMouseOver:function(route,polyline){if(!route.isSelected()){this.highlight(route,polyline);if(this.options.legend){this.setSelection(route);L.DomUtil.get("legend_"+route._leaflet_id).parentNode.previousSibling.click()}}this.fire("route_mouseover",{route:route,polyline:polyline})},_onRouteMouseOut:function(route,polyline){if(!route.isSelected())this.unhighlight(route,
11
- polyline);this.fire("route_mouseout",{route:route,polyline:polyline})},_onRouteClick:function(route,polyline){this.setSelection(route)},_onRouteSelected:function(route,polyline){if(!route.isSelected())this.unhighlight(route,polyline)},_onRouteLoaded:function(route){if(this.options.legend)this._legend.addBaseLayer(route,'<svg id="legend_'+route._leaflet_id+'" width="25" height="10" version="1.1" xmlns="http://www.w3.org/2000/svg">'+'<line x1="0" x2="50" y1="5" y2="5" stroke="'+route.options.polyline_options.color+
12
- '" fill="transparent" stroke-width="5" /></svg>'+" "+route.get_name());this.fire("route_loaded",{route:route});if(++this._loadedCount===this._tracks.length){this.fire("loaded");if(this.options.flyToBounds)this._map.flyToBounds(this.getBounds(),{duration:.25,easeLinearity:.25,noMoveStart:true});if(this.options.legend)this._legend.addTo(this._map)}},_onSelectionChanged:function(e){var elevation=this._elevation;var eleDiv=elevation.getContainer();var route=this.getSelection();elevation.clear();if(route&&
13
- route.isSelected()){if(!eleDiv)elevation.loadChart(this._map);route.getLayers().forEach(function(layer){if(layer instanceof L.Polyline){elevation.addData(layer);layer.bringToFront()}})}else if(eleDiv)elevation.remove()},_onLegendSelected:function(e){var layers=this._layers._layers;var route=e.layer;if(!route.isSelected()){this.setSelection(route);for(var i in route._layers)this.highlight(route,route._layers[i]);this._map.flyToBounds(e.layer.getBounds())}var parent=e.input.closest(".leaflet-control-layers-list");
14
- var offset=e.input.offsetTop-parent.offsetTop;parent.scroll({top:offset||0,behavior:"smooth"});for(var j in layers){var selected=layers[j].isSelected();var legend=L.DomUtil.get("legend_"+layers[j]._leaflet_id);legend.querySelector("line").style.stroke=selected?this.options.highlight.color:"";legend.parentNode.style.fontWeight=selected?"700":""}},_get:function(url,callback){var xhr=new XMLHttpRequest;xhr.open("GET",url);xhr.onreadystatechange=function(){if(xhr.readyState===XMLHttpRequest.DONE&&xhr.status===
15
- 200)callback(xhr.response)};xhr.send()},_uniqueColors:function(count){if(count===0)return[];if(count===1)return["#0000ff"];var increment=1/count;var colors=[];for(var i=0;i<count;++i){var hue=i*increment;var rgb=this._hsvToRgb(hue,1,.7);var hex="#"+this._rgbToHex(rgb[0],rgb[1],rgb[2]);colors.push(hex)}return colors},_hsvToRgb:function(h,s,v){var r,g,b;var i=Math.floor(h*6);var f=h*6-i;var p=v*(1-s);var q=v*(1-f*s);var t=v*(1-(1-f)*s);switch(i%6){case 0:r=v;g=t;b=p;break;case 1:r=q;g=v;b=p;break;case 2:r=
16
- p;g=v;b=t;break;case 3:r=p;g=q;b=v;break;case 4:r=t;g=p;b=v;break;case 5:r=v;g=p;b=q;break}return[r*255,g*255,b*255]},_rgbToHex:function(r,g,b){return this._byteToHex(r)+this._byteToHex(g)+this._byteToHex(b)},_byteToHex:function(n){return(n>>4&15).toString(16)+(n&15).toString(16)},removeFrom:function(map){this._layers.removeFrom(map)}});L.GpxGroup.include(L.Mixin.Events);L.GpxGroup.include(L.Mixin.Selection);L.gpxGroup=function(tracks,options){return new L.GpxGroup(tracks,options)};
4
+ L.control.layersLegend=function(baseLayers,overlays,options){return new L.Control.LayersLegend(baseLayers,overlays,options)};L.GeoJSON.include(L.Mixin.Selectable);
5
+ L.GpxGroup=L.Class.extend({options:{highlight:{color:"#ff0",opacity:1},points:[],points_options:{icon:{iconUrl:"../images/elevation-poi.png",iconSize:[12,12]}},flyToBounds:true,legend:false,legend_options:{position:"topright",collapsed:false},elevation:true,elevation_options:{theme:"yellow-theme",detachedView:true,elevationDiv:"#elevation-div"},distanceMarkers:true,distanceMarkers_options:{lazy:true}},initialize:function(tracks,options){L.Util.setOptions(this,options);this._count=0;this._loadedCount=
6
+ 0;this._tracks=tracks;this._routes={};this._layers=L.featureGroup();this._markers=L.featureGroup();this._elevation=L.control.elevation(this.options.elevation_options);this._legend=L.control.layersLegend(null,null,this.options.legend_options);var icon=L.icon(this.options.points_options.icon);this.options.points.forEach(function(poi){var marker=L.marker(poi.latlng,{icon:icon});marker.bindTooltip(poi.name,{direction:"auto"});marker.addTo(this._markers)},this);if(typeof L.DistanceMarkers==="undefined")this.options.distanceMarkers=
7
+ false},getBounds:function(){return this._layers.getBounds()},addTo:function(map){this._layers.addTo(map);this._markers.addTo(map);this._map=map;this.on("selection_changed",this._onSelectionChanged,this);this._map.on("legend_selected",this._onLegendSelected,this);this._tracks.forEach(this.addTrack,this)},addTrack:function(track){fetch(track).then((response)=>{if(response.ok)response.text().then((doc)=>{let geojson;doc=doc.trim();try{let xml=(new DOMParser).parseFromString(doc,"text/xml");let type=
8
+ xml.documentElement.tagName.toLowerCase();let name=xml.getElementsByTagName("name");if(!(type in toGeoJSON))type=xml.documentElement.tagName=="TrainingCenterDatabase"?"tcx":"gpx";geojson=toGeoJSON[type](xml);geojson.name=name.length>0?Array.from(name).find((tag)=>tag.parentElement.tagName=="trk").textContent:""}catch(e){try{geojson=JSON.parse(doc.toString())}catch(e){console.warn("Error parsing track: "+track)}}if(!geojson.name)geojson.name=track.split("/").pop().split("#")[0].split("?")[0];this._loadRoute(geojson)})})},
9
+ _loadRoute:function(data){if(!data)return;var colors=this._uniqueColors(this._tracks.length);var color=colors[this._count++];var line_style={color:color,opacity:.75,weight:5,distanceMarkers:this.options.distanceMarkers_options};var route=L.geoJson(data,{name:data.name||"",style:(feature)=>line_style,distanceMarkers:line_style.distanceMarkers,originalStyle:line_style,filter:(feature)=>feature.geometry.type!="Point"});route.addTo(this._layers);this._routes[route._leaflet_id]=route;route.eachLayer((layer)=>
10
+ this._onRouteLayer(route,layer));this._onRouteLoaded(route)},_onRouteLayer:function(route,layer){var polyline=layer;route.on("selected",L.bind(this._onRouteSelected,this,route,polyline));polyline.on("mouseover",L.bind(this._onRouteMouseOver,this,route,polyline));polyline.on("mouseout",L.bind(this._onRouteMouseOut,this,route,polyline));polyline.on("click",L.bind(this._onRouteClick,this,route,polyline));polyline.bindTooltip(route.options.name,{direction:"auto",sticky:true})},highlight:function(route,
11
+ polyline){polyline.setStyle(this.options.highlight);if(this.options.distanceMarkers)polyline.addDistanceMarkers()},unhighlight:function(route,polyline){polyline.setStyle(route.options.originalStyle);if(this.options.distanceMarkers)polyline.removeDistanceMarkers()},_onRouteMouseOver:function(route,polyline){if(!route.isSelected()){this.highlight(route,polyline);if(this.options.legend){this.setSelection(route);L.DomUtil.get("legend_"+route._leaflet_id).parentNode.previousSibling.click()}}this.fire("route_mouseover",
12
+ {route:route,polyline:polyline})},_onRouteMouseOut:function(route,polyline){if(!route.isSelected())this.unhighlight(route,polyline);this.fire("route_mouseout",{route:route,polyline:polyline})},_onRouteClick:function(route,polyline){this.setSelection(route)},_onRouteSelected:function(route,polyline){if(!route.isSelected())this.unhighlight(route,polyline)},_onRouteLoaded:function(route){if(this.options.legend)this._legend.addBaseLayer(route,'<svg id="legend_'+route._leaflet_id+'" width="25" height="10" version="1.1" xmlns="http://www.w3.org/2000/svg">'+
13
+ '<line x1="0" x2="50" y1="5" y2="5" stroke="'+route.options.originalStyle.color+'" fill="transparent" stroke-width="5" /></svg>'+" "+route.options.name);this.fire("route_loaded",{route:route});if(++this._loadedCount===this._tracks.length){this.fire("loaded");if(this.options.flyToBounds)this._map.flyToBounds(this.getBounds(),{duration:.25,easeLinearity:.25,noMoveStart:true});if(this.options.legend)this._legend.addTo(this._map)}},_onSelectionChanged:function(e){var elevation=this._elevation;var eleDiv=
14
+ elevation.getContainer();var route=this.getSelection();elevation.clear();if(route&&route.isSelected()){if(!eleDiv)elevation.loadChart(this._map);route.getLayers().forEach(function(layer){if(layer instanceof L.Polyline){elevation.addData(layer,false);layer.bringToFront()}})}else if(eleDiv)elevation.remove()},_onLegendSelected:function(e){var layers=this._layers._layers;var route=e.layer;if(!route.isSelected()){this.setSelection(route);for(var i in route._layers)this.highlight(route,route._layers[i]);
15
+ this._map.flyToBounds(e.layer.getBounds())}var parent=e.input.closest(".leaflet-control-layers-list");var offset=e.input.offsetTop-parent.offsetTop;parent.scroll({top:offset||0,behavior:"smooth"});for(var j in layers){var selected=layers[j].isSelected();var legend=L.DomUtil.get("legend_"+layers[j]._leaflet_id);legend.querySelector("line").style.stroke=selected?this.options.highlight.color:"";legend.parentNode.style.fontWeight=selected?"700":""}},_uniqueColors:function(count){if(count===0)return[];
16
+ if(count===1)return["#0000ff"];var increment=1/count;var colors=[];for(var i=0;i<count;++i){var hue=i*increment;var rgb=this._hsvToRgb(hue,1,.7);var hex="#"+this._rgbToHex(rgb[0],rgb[1],rgb[2]);colors.push(hex)}return colors},_hsvToRgb:function(h,s,v){var r,g,b;var i=Math.floor(h*6);var f=h*6-i;var p=v*(1-s);var q=v*(1-f*s);var t=v*(1-(1-f)*s);switch(i%6){case 0:r=v;g=t;b=p;break;case 1:r=q;g=v;b=p;break;case 2:r=p;g=v;b=t;break;case 3:r=p;g=q;b=v;break;case 4:r=t;g=p;b=v;break;case 5:r=v;g=p;b=q;
17
+ break}return[r*255,g*255,b*255]},_rgbToHex:function(r,g,b){return this._byteToHex(r)+this._byteToHex(g)+this._byteToHex(b)},_byteToHex:function(n){return(n>>4&15).toString(16)+(n&15).toString(16)},removeFrom:function(map){this._layers.removeFrom(map)}});L.GpxGroup.include(L.Mixin.Events);L.GpxGroup.include(L.Mixin.Selection);L.gpxGroup=function(tracks,options){return new L.GpxGroup(tracks,options)};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raruto/leaflet-elevation",
3
- "version": "1.7.0",
3
+ "version": "1.7.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/chart.js CHANGED
@@ -97,8 +97,10 @@ export var Chart = L.Class.extend({
97
97
 
98
98
  clear: function() {
99
99
  this._resetDrag();
100
+ this._hideDiagramIndicator()
100
101
  this._area.selectAll('path').attr("d", "M0 0");
101
102
  this._context.clearRect(0, 0, this._width(), this._height());
103
+
102
104
  if (this._path) {
103
105
  // this._x.domain([0, 1]);
104
106
  // this._y.domain([0, 1]);
@@ -173,6 +175,7 @@ export var Chart = L.Class.extend({
173
175
  */
174
176
  _initBrush: function(e) {
175
177
  const brush = (e) => {
178
+ if (!this._data.length) return;
176
179
  let extent = e.selection;
177
180
  if (extent) {
178
181
  let start = this._findIndexForXCoord(extent[0]);
@@ -182,6 +185,7 @@ export var Chart = L.Class.extend({
182
185
  }
183
186
 
184
187
  const focus = (e) => {
188
+ if (!this._data.length) return;
185
189
  if (e.type == 'brush' && !e.sourceEvent) return;
186
190
  let rect = this._chart.panes.brush.select('.overlay').node();
187
191
  let coords = d3.pointers(e, rect)[0];
package/src/components.js CHANGED
@@ -506,6 +506,12 @@ export const Chart = ({
506
506
 
507
507
  g.attr("transform", "translate(" + margins.left + "," + margins.top + ")");
508
508
 
509
+ // Partially fix: https://github.com/Raruto/leaflet-elevation/issues/123
510
+ if(/Mac|iPod|iPhone|iPad/.test(navigator.platform) && /AppleWebkit/i.test(navigator.userAgent)) {
511
+ canvas
512
+ .style("transform", "translate(" + margins.left + "px," + margins.top + "px)");
513
+ }
514
+
509
515
  clipRect
510
516
  .attr("x", 0)
511
517
  .attr("y", 0)
package/src/control.js CHANGED
@@ -17,13 +17,13 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
17
17
  __TOGEOJSON: 'https://unpkg.com/@tmcw/togeojson@4.3.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.7.0/libs/leaflet-distance-marker.min.js',
20
+ __LDISTANCEM: 'https://unpkg.com/@raruto/leaflet-elevation@1.7.3/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
24
24
  */
25
25
  addData: function(d, layer) {
26
- if ((typeof layer === "undefined" || layer === null) && d.on) {
26
+ if (typeof layer === "undefined" && d.on) {
27
27
  layer = d;
28
28
  }
29
29
  Elevation._d3LazyLoader = _.lazyLoader(
@@ -56,13 +56,40 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
56
56
  */
57
57
  clear: function() {
58
58
  if (this._marker) this._marker.remove();
59
- if (this._chart) this._chart.clear();
60
- if (this._layers) this._layers.clearLayers();
59
+ if (this._chart) this._clearChart();
60
+ if (this._layers) this._clearLayers();
61
+ if (this._markers) this._clearMarkers();
61
62
 
62
63
  this._data = [];
63
64
  this.track_info = {};
64
65
 
65
66
  this._fireEvt("eledata_clear");
67
+
68
+ this._updateChart();
69
+ },
70
+
71
+ _clearChart: function() {
72
+ if (this._events && this._events.elechart_updated) {
73
+ this._events.elechart_updated.forEach(e => this.off('elechart_updated', e.fn, e.ctx));
74
+ }
75
+ if (this._chart && this._chart._container) {
76
+ this._chart._container.selectAll('g.point .point').remove();
77
+ this._chart.clear();
78
+ }
79
+ },
80
+
81
+ _clearLayers: function() {
82
+ if (this._layers && this._layers.eachLayer) {
83
+ this._layers.eachLayer(l => l.remove())
84
+ this._layers.clearLayers();
85
+ }
86
+ },
87
+
88
+ _clearMarkers: function() {
89
+ if (this._markers && this._markers.eachLayer) {
90
+ this._markers.eachLayer(l => l.remove())
91
+ this._markers.clearLayers();
92
+ }
66
93
  },
67
94
 
68
95
  /**
@@ -142,6 +169,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
142
169
  initialize: function(options) {
143
170
  this._data = [];
144
171
  this._layers = L.featureGroup();
172
+ this._markers = L.featureGroup();
145
173
  this._markedSegments = L.polyline([]);
146
174
  this._chartEnabled = true,
147
175
 
@@ -168,6 +196,8 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
168
196
  // this._updateSummary = _.debounce(this._updateSummary, 300, this);
169
197
  // }
170
198
 
199
+ if (this.options.wptIcons === true) this.options.wptIcons = Options.wptIcons;
200
+
171
201
  // Leaflet canvas renderer colors
172
202
  L.extend(D3.Colors, this.options.colors || {});
173
203
  },
@@ -420,7 +450,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
420
450
  */
421
451
  _addGPXData: function(coords) {
422
452
  _.each(coords, point => {
423
- this._addPoint(point.lat, point.lng, point.meta.ele);
453
+ this._addPoint(point.lat, point.lng, typeof point.alt !== "undefined" ? point.alt : point.meta.ele);
424
454
  this._fireEvt("elepoint_added", { point: point, index: this._data.length - 1 });
425
455
  });
426
456
  this._fireEvt("eletrack_added", { coords: coords, index: this._data.length - 1 });
@@ -448,6 +478,10 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
448
478
  if (layer) this._layers.addLayer(layer)
449
479
  },
450
480
 
481
+ _addMarker: function(marker) {
482
+ if (marker) this._markers.addLayer(marker)
483
+ },
484
+
451
485
  /**
452
486
  * Adds the control to the given "detached" div.
453
487
  */
@@ -886,7 +920,7 @@ export const Elevation = L.Control.Elevation = L.Control.extend({
886
920
  * Calculates [x, y] domain and then update chart.
887
921
  */
888
922
  _updateChart: function() {
889
- if (!this._data.length || !this._container) return;
923
+ if (/*!this._data.length ||*/ !this._container) return;
890
924
 
891
925
  this._fireEvt("elechart_axis");
892
926
  this._fireEvt("elechart_area");
package/src/index.css CHANGED
@@ -33,6 +33,7 @@
33
33
  overflow: visible;
34
34
  display: block;
35
35
  touch-action: none;
36
+ user-select: none;
36
37
  }
37
38
 
38
39
  .elevation-control.elevation .background.zoomed .point,
package/src/options.js CHANGED
@@ -18,22 +18,6 @@ export var Options = {
18
18
  elevationDiv: "#elevation-div",
19
19
  followMarker: true,
20
20
  forceAxisBounds: false,
21
- gpxOptions: {
22
- async: true,
23
- marker_options: {
24
- startIconUrl: null,
25
- endIconUrl: null,
26
- shadowUrl: null,
27
- wptIcons: {
28
- '': L.divIcon({
29
- className: 'elevation-waypoint-marker',
30
- html: '<i class="elevation-waypoint-icon"></i>',
31
- iconSize: [30, 30],
32
- iconAnchor: [8, 30],
33
- })
34
- },
35
- },
36
- },
37
21
  height: 200,
38
22
  heightFactor: 1,
39
23
  imperial: false,
@@ -89,6 +73,15 @@ export var Options = {
89
73
  sRange: undefined,
90
74
  width: 600,
91
75
  waypoints: true,
76
+ wptIcons: {
77
+ '': L.divIcon({
78
+ className: 'elevation-waypoint-marker',
79
+ html: '<i class="elevation-waypoint-icon"></i>',
80
+ iconSize: [30, 30],
81
+ iconAnchor: [8, 30],
82
+ }),
83
+ },
84
+ wptLabels: true,
92
85
  xAttr: "dist",
93
86
  xLabel: "km",
94
87
  xTicks: undefined,
package/src/utils.js CHANGED
@@ -79,7 +79,8 @@ export function GeoJSONLoader(data, control) {
79
79
  : L.extend({ lazy: true }, control.options.distanceMarkers)
80
80
  );
81
81
 
82
- let wptIcons = control.options.gpxOptions.marker_options.wptIcons;
82
+ let wptIcons = control.options.wptIcons;
83
+ let wptLabels = control.options.wptLabels;
83
84
 
84
85
  let layer = L.geoJson(data, {
85
86
  distanceMarkers: distanceMarkers,
@@ -91,27 +92,40 @@ export function GeoJSONLoader(data, control) {
91
92
  return style;
92
93
  },
93
94
  pointToLayer: (feature, latlng) => {
94
- if (!control.options.waypoints) return;
95
+ if (!control.options.waypoints) return;
95
96
 
96
97
  let prop = feature.properties;
97
- let desc = prop.desc ? prop.desc : '';
98
- let name = prop.name ? prop.name : '';
99
- let sym = (prop.sym ? prop.sym : name).replace(' ', '-').replace('"', '').replace("'", '').toLowerCase();
100
-
101
- // generate appropriate icon symbol or retrieve it from cache
102
- wptIcons[sym] = (wptIcons[sym] ? wptIcons[sym] : L.divIcon({
103
- className: 'elevation-waypoint-marker',
104
- html: '<i class="elevation-waypoint-icon ' + sym + '"></i>',
105
- iconSize: [30, 30],
106
- iconAnchor: [8, 30],
107
- }));
108
- let marker = L.marker(latlng, { icon: wptIcons[sym] });
109
- if (name || desc) {
110
- marker.bindPopup("<b>" + name + "</b>" + (desc.length > 0 ? '<br>' + desc : '')).openPopup();
111
- }
112
- control._registerCheckPoint({latlng: latlng, label: name}, true);
113
- control.fire('waypoint_added', { point: marker, element: latlng, properties: prop });
114
- return marker;
98
+ let desc = prop.desc ?? '';
99
+ let name = prop.name ?? '';
100
+ let sym = (prop.sym ?? name).replace(' ', '-').replace('"', '').replace("'", '').toLowerCase();
101
+
102
+ // Handle waypoint lables (markers and dots)
103
+ if (!wptLabels) {
104
+ name = '';
105
+ desc = '';
106
+ }
107
+
108
+ // Handle chart waypoints (dots)
109
+ if ([true, "dots"].includes(control.options.waypoints)) {
110
+ control._registerCheckPoint({latlng: latlng, label: name}, true);
111
+ }
112
+
113
+ // Handle map waypoints (markers)
114
+ if ([true, "markers"].includes(control.options.waypoints) && wptIcons != false) {
115
+ // generate and cache appropriate icon symbol
116
+ if (!wptIcons.hasOwnProperty(sym)) {
117
+ wptIcons[sym] = L.divIcon(
118
+ L.extend({}, wptIcons[""].options, { html: '<i class="elevation-waypoint-icon ' + sym + '"></i>' } )
119
+ );
120
+ }
121
+ let marker = L.marker(latlng, { icon: wptIcons[sym] });
122
+ if (name || desc) {
123
+ marker.bindPopup("<b>" + name + "</b>" + (desc.length > 0 ? '<br>' + desc : '')).openPopup();
124
+ }
125
+ control._addMarker(marker);
126
+ control.fire('waypoint_added', { point: marker, element: latlng, properties: prop });
127
+ return marker;
128
+ }
115
129
  },
116
130
  onEachFeature: (feature, layer) => {
117
131
  if (feature.geometry && feature.geometry.type == 'Point') return;