mpetazzoni / leaflet-gpx

A GPX track plugin for Leaflet.js
http://mpetazzoni.github.io/leaflet-gpx
BSD 2-Clause "Simplified" License
550 stars 119 forks source link

GPX with Leaflet.PolylineOffset #95

Closed southmedia closed 4 years ago

southmedia commented 4 years ago

Hi, is it possible to use PolylineOffset with a GPX track, because that plugin adds offset capabilities to a L.Polyline class.? I intend to add a parallel line to the track path. In that case, is there any code example o demo I can take a look to? Thank you!

mpetazzoni commented 4 years ago

@southmedia It seems like the PolylineOffset plugin (https://github.com/bbecquet/Leaflet.PolylineOffset#usage) directly modifies the behavior of the L.Polyline class. Since leaflet-gpx simply creates the polyline by doing new L.Polyline(...), I believe the two should work just fine together.

To set the offset, you can either pass it through the polyline_options when initializing the L.GPX layer:

var gpx = new L.GPX(url, {
  polyline_options: {
    offset: 42
  }
});

Or, you can set the offset after the fact. leaflet-gpx fires an addline event with the polyline in the event data:

var gpx = new L.GPX(url, ...).on('addline', function(event) {
  event.line.setOffset(42);
});
southmedia commented 4 years ago

Hi Maxime, tried with first option and works smoothly.

var gpx = new L.GPX(url, { polyline_options: { offset: 42 } }); Thank you again!