Closed donostiastudio closed 6 months ago
You'll want to extend the bounds each time, instead of fitting to the latest loaded layer's bounds:
var bounds = null;
...
.on('loaded', function(e) {
var layerBounds = e.target.getBounds();
if (bounds === null) {
bounds = layerBounds;
} else {
bounds.extend(layerBounds);
}
map.fitBounds(bounds);
});
Thank you. Could you help me with the code? I'm not very good at JavaScript. I tried to fix the code as you see below, but it's not working. The map no longer displays the GPX files
<script>
var map = L.map('map').setView([41.90, 12.49], 7); //
var bounds = null;
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
// Array GPX files
var gpxFiles = [
// Inserisci qui i percorsi GPX
<?php foreach ($files_gpx as $file_gpx) { ?>
'<?php echo $file_gpx;?>',
<?php } ?>
];
// loads gpx files
gpxFiles.forEach(function(gpxFile) {
new L.GPX(gpxFile, {
async: true,
polyline_options: {
color: '#683600',
opacity: 0.75,
weight: 5,
lineCap: 'round'
},
gpx_options: {
joinTrackSegments: true
},
marker_options: {
startIconUrl: '<?php echo get_stylesheet_directory_uri(); ?>/images/partenza.svg',
endIconUrl: '<?php echo get_stylesheet_directory_uri(); ?>/images/partenza.svg',
shadowUrl: ''
}
}).on('loaded', function(e) {
var layerBounds = e.target.getBounds();
if (bounds === null) {
bounds = layerBounds;
} else {
bounds.extend(layerBounds);
}
map.fitBounds(bounds);
});
});
</script>
Any javascript errors in the console? Can you confirm the loaded
event handler is being called (by debugging, or adding a console.log(...)
statement) ?
Thank you so much for your help. I've put WordPress in debug mode. In the console, I'm getting these alerts, but I don't seem to see any errors.
As you can see, with the modification I made trying to follow your instructions, the map is loaded but there are no GPX tracks. Did I modify the code correctly?
THX!
I'd recommend you use the Javascript debugger in your browser with a breakpoint in the loaded
event handler, and step-by-step through it to see what happens.
Hi there! thank you I solved. This is the working code
`
// Creazione della mappa Leaflet
var map = L.map('map').setView([41.90, 12.49], 5); // Imposta la posizione iniziale della mappa
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);
// Creazione di un layer per le tracce GPX
var gpxLayer = L.geoJson().addTo(map);
// Funzione da eseguire quando tutte le tracce GPX sono caricate
function onAllGpxLoaded() {
map.fitBounds(gpxLayer.getBounds()); // Centra la mappa sui percorsi GPX
}
// Array contenente i percorsi dei file GPX
// Array di percorsi GPX
var gpxFiles = [
// Inserisci qui i percorsi GPX
<?php foreach ($files_gpx as $file_gpx) { ?>
'<?php echo $file_gpx;?>',
<?php } ?>
];
// Contatore per tenere traccia del caricamento dei file GPX
var gpxCount = 0;
// Funzione per caricare i file GPX sulla mappa
function loadGPX(file) {
new L.GPX(file, {
async: true,
polyline_options: {
color: '#683600',
opacity: 0.75,
weight: 5,
lineCap: 'round'
},
gpx_options: {
joinTrackSegments: true
},
marker_options: {
startIconUrl: '<?php echo get_stylesheet_directory_uri(); ?>/images/partenza.svg',
endIconUrl: '<?php echo get_stylesheet_directory_uri(); ?>/images/partenza.svg',
shadowUrl: ''
}
}).on('loaded', function(e) {
gpxLayer.addLayer(e.target);
gpxCount++;
// Verifica se tutti i file GPX sono stati caricati
if (gpxCount === gpxFiles.length) {
onAllGpxLoaded(); // Esegui la funzione quando tutti i file sono stati caricati
}
}).addTo(map);
}
// Carica tutti i file GPX
gpxFiles.forEach(function(file) {
loadGPX(file);
});
</script>`
See it in action https://www.abbondantiedozzinali.it/fondo-stradale/ghiaia/
Hi there! I have a map with several GPX files. I would like the map to correctly center on all the tracks after loading. However, it currently centers only on the last uploaded route. You can see it in action here: https://www.abbondantiedozzinali.it/fondo-stradale/ghiaia/
Here's the code:
`
Could you help me? Thank you