Newer
Older
website / org / gpx / 20260927_biciclettata / js / gpx.js
var map = L.map('map');

// L.tileLayer('https://{s}.tile-cyclosm.openstreetmap.fr/cyclosm-lite/{z}/{x}/{y}.png', { // not working, cyclosm should be self-hosted
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    minZoom: 6, // avoid zooming outside track range, roughly, to prevent extra tile requests
    maxZoom: 15,
    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, desc) {
    new L.GPX(gpx, {
            async: true,
            marker_options: {
                startIconUrl: '../gpx/20260314_london_italy/images/pin-icon-start.png',
                endIconUrl: '../gpx/20260314_london_italy/images/pin-icon-end.png',
                shadowUrl: '../gpx/20260314_london_italy/images/pin-shadow.png'
            },
            polyline_options: {
                smoothFactor: 3, // simplify track at each zoom level, uses the simplify() Leaflet.js call (Ramer-Douglas-Peucker)
                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);

    // setTimeout(function(){ map.invalidateSize()}, 500);
}

addGPX(map, '../gpx/20260927_biciclettata/compressed/biciclettata.gpx', 'purple')