Closed fschn90 closed 1 year ago
I use leaflet-elavation in a WordPress plugin. There it is possible to have multiple profiles on one page. But I don't see in your code why it doesn't work. However, the tracks I used for testing are not so long as yours.
@flo-schn did you try using one of the files in the /examples
folder as a starting point (ie. just changing the elevation settings and the gpx file as your need)
Sometimes setting these meta parameters differently can cause problems:
👋 Raruto
hey, just did and issue persists.
see screenshot:
when five or more maps on a page the graph doesnt use the entire elevation-div anymore and leaves some space unused at the bottom.
best, flo
I have 7 maps on a page (same track) - no problem. In my tests I have a page with 5 different maps, no problem also.
I think the bug is due to that the following function L.setOptions
doesn't deep merge between the default and currently provided options (ref: nested objects):
so that this value options.margins.bottom
is increased with each new instance of the class:
Below some code that recreates the above situation:
<!DOCTYPE html>
<html>
<head>
<title>leaflet-elevation.js</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<link rel="dns-prefetch" href="https://tile.openstreetmap.org">
<link rel="dns-prefetch preconnect" href="https://unpkg.com" />
<link rel="preload" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js" as="script">
<link rel="preload" href="https://unpkg.com/leaflet-ui@0.5.9/dist/leaflet-ui.js" as="script">
<style> @import 'https://unpkg.com/@raruto/leaflet-elevation@2.2.8/libs/fullpage.css';</style>
<!-- leaflet-ui -->
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet-ui@0.5.9/dist/leaflet-ui.js"></script>
<!-- leaflet-elevation -->
<link rel="stylesheet" href="https://unpkg.com/@raruto/leaflet-elevation@2.2.8/dist/leaflet-elevation.min.css" />
<script src="https://unpkg.com/@raruto/leaflet-elevation@2.2.8/dist/leaflet-elevation.js"></script>
</head>
<body style="flex-direction: row; flex-wrap: wrap; gap: 10px;">
<div id="map" class="leaflet-map"></div>
<div id="map1" class="leaflet-map"></div>
<div id="map2" class="leaflet-map"></div>
<div id="map3" class="leaflet-map"></div>
<div id="map4" class="leaflet-map"></div>
<div id="map5" class="leaflet-map"></div>
<div id="map6" class="leaflet-map"></div>
<div id="map7" class="leaflet-map"></div>
<div id="map8" class="leaflet-map"></div>
<div id="map9" class="leaflet-map"></div>
<div id="map10" class="leaflet-map"></div>
<script>
let opts = {
map: {
center: [41.4583, 12.7059],
zoom: 5,
fullscreenControl: false,
resizerControl: true,
preferCanvas: true,
rotate: true,
// bearing: 45,
rotateControl: {
closeOnZeroBearing: true
},
},
elevationControl: {
url: "https://raruto.github.io/leaflet-elevation/examples/via-emilia.gpx",
options: {
theme: "lightblue-theme",
collapsed: false,
autohide: false,
autofitBounds: true,
position: "bottomleft",
detached: true,
summary: "inline",
imperial: false,
// altitude: "disabled",
slope: "disabled",
speed: false,
acceleration: false,
time: "summary",
legend: true,
followMarker: true,
almostOver: true,
distanceMarkers: false,
hotline: false,
},
},
layersControl: {
options: {
collapsed: false,
},
},
};
L.Control.Elevation.addInitHook(function(){
console.log(this.options.margins); // <-- default margins: { top: 30, right: 30, bottom: 60, left: 40 }
});
for(id of ['map', 'map1', 'map2', 'map3', 'map4', 'map5', 'map6', 'map7', 'map8', 'map9', 'map10']) {
let map = L.map(id, opts.map);
let controlElevation = L.control.elevation(opts.elevationControl.options).addTo(map);
let controlLayer = L.control.layers(null, null, opts.layersControl.options);
controlElevation.on('eledata_loaded', ({ layer, name }) => controlLayer.addTo(map) && layer.eachLayer((trkseg) => trkseg.feature.geometry.type != "Point" && controlLayer.addOverlay(trkseg, trkseg.feature && trkseg.feature.properties && trkseg.feature.properties.name || name)));
controlElevation.load('via-emilia.gpx');
}
</script>
<a href="https://github.com/Raruto/leaflet-elevation" class="view-on-github" style="position: fixed;top: 30px;left: calc(50% - 81px);z-index: 9999;"> <img alt="View on Github" src="https://raruto.github.io/img/view-on-github.png" title="View on Github" width="163"> </a>
</body>
</html>
I have 7 maps on a page (same track) - no problem. In my tests I have a page with 5 different maps, no problem also.
Could it also be that you always pass all the available options? (so in this case the reference to the options.margins
is not taken from the default one but it is always created as a new one?)
👋 Raruto
Could it also be that you always pass all the available options? (so in this case the reference to the options.margins is not taken from the default one but it is always created as a new one?)
You are right:
Maybe I coded this for the same reason, but didn't remember that.
Ok, so here's a simple workaround:
// Related to: https://github.com/Raruto/leaflet-elevation/issues/238#issuecomment-1493717835
// let controlElevation = L.control.elevation(opts.elevationControl.options).addTo(map);
let controlElevation = L.control.elevation({
...opts.elevationControl.options,
margins: { top: 30, right: 30, bottom: 60, left: 40 } // prevent keeping a reference of default margins object
}).addTo(map);
thats it, does the trick. works
appreciate the support, thanks very much!!
I reopen, because this is a bug.. 🐞 (until someone finds a solution)
multiple maps and elevation divs on one page lead to pecular illustration
Please see screenshots:
Your environment
Steps to reproduce
Expected behaviour
an elevation graph using the entire space available.
Actual behaviour
the elevation graph being too small and using only a fraction of the space available. see screenshots above.