Raruto / leaflet-elevation

Leaflet plugin that allows to add elevation profiles using d3js
GNU General Public License v3.0
255 stars 85 forks source link

Fix bug when layer have no _path #58

Closed gegeweb closed 4 years ago

gegeweb commented 4 years ago

Add L.polyline data (array of L.latLng) as gpx data to the chart.

Raruto commented 4 years ago

Hi Gérald,

could you kindly:

  1. use the tab character as indentation? (so we can easily distinguish your changes...)
  2. provide a more detailed description or example of your changes?

Thanks for your cooperation, Raruto

gegeweb commented 4 years ago

Hope last commit is now fine!

Changes are:

I'm using the plugin with a feature group, so the layer have no path.

Hope i'm clear. ;)

gegeweb commented 4 years ago

Here you can check the code where i use this:

.on('routeselected', function(e) {
  let coordinates = e.route.coordinates;
  elevationControl.clear();
  elevationControl.show();
  elevationControl.addData({ '_latlngs': coordinates }, controlRouting.getLine());
})

coordinates is an array of

  L.latLng object: { lat: lat, lon: lon, alt: z }

and the layers returned by the function a

L.FeatureGroup object
Raruto commented 4 years ago

Here you can see a simple example on how to effectively use an L.polyline as elevation object.

In your particular case, it could also be solved in this way:

let _latlngs = e.route.coordinates; // same as polyline.getLatLngs()
let coords = L.GeoJSON.latLngsToCoords(e.route.coordinates);

elevationControl.addData({type: "Feature", geometry: { coordinates: coords, type: "LineString" }}, controlRouting.getLine());

Your solution is also quite correct, but I think it would be better if we adopt a slightly cleaner / general one.

gegeweb commented 4 years ago

Yes you're right, this solution works fine, thanks ! So there is no need to modify addGPXData.

But we always need to check if layer have _path propertie in _clearPath or where adding class.

I'll update the PR by removing what's useless.

gegeweb commented 4 years ago

shorter, single line:

elevationControl.addData(L.polyline(e.route.coordinates).toGeoJSON(), controlRouting.getLine());

Thanks again, it looks better. ;)