Newer
Older
gpx_web_viewer / js / gpx.js
var map = L.map('map');

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: 'Map data &copy; <a href="http://www.osm.org">OpenStreetMap</a>'
}).addTo(map);

function download(url) {
  const a = document.createElement('a')
  a.href = url
  a.download = url.split('/').pop()
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
}

function addGPX(map, gpx, lineColor) {
    new L.GPX(gpx, {
            async: true,
            marker_options: {
                startIconUrl: 'images/pin-icon-start.png',
                endIconUrl: 'images/pin-icon-end.png',
                shadowUrl: 'images/pin-shadow.png'
            },
            polyline_options: {
                color: lineColor,
                opacity: 1,
                weight: 3,
                lineCap: 'round'
            }
    }).on('loaded', function(e) {
        map.fitBounds(e.target.getBounds());
    }).on('mouseover', function(e) {
        var paths = gpx.split("/")
        this.bindTooltip(
            "Click to download: " + paths[paths.length - 1],
            { sticky: true }
        )
        this.openPopup(e.latlng);
    }).on('mouseout', function(e) {
        setTimeout(this.closePopup(), 500);
    }).on('click', function(e) {
        setTimeout(this.closePopup(), 500);
        download(gpx);
    }).addTo(map);
}

addGPX(map, '../tracks/turin_09122022.gpx', 'red')
addGPX(map, '../tracks/biella_27122022.gpx', 'blue')