Closed mjut closed 3 years ago
Is this the sort of thing you mean https://mappy.dabbles.info/ ? This is a little site I do for my walks, and it dynamically sizes/zooms to the range.
If so, feel free to dig through the code, I think the main bit is...
// outside of loop
var maxBounds = L.bounds();
//inside loop
var myBounds = e.target.getBounds();
if(typeof maxBounds == 'undefined' ) {
maxBounds = e.target.getBounds();
}
if( (typeof maxBounds.getSouthWest().lat == 'undefined') || myBounds.getSouthWest().lat < maxBounds.getSouthWest().lat ) {
maxBounds._southWest['lat'] = myBounds._southWest.lat;
}
if( (typeof maxBounds.getSouthWest().lng == 'undefined') || myBounds.getSouthWest().lng < maxBounds.getSouthWest().lng ) {
maxBounds._southWest['lng'] = myBounds._southWest.lng;
}
if( (typeof maxBounds.getNorthEast().lat == 'undefined') || myBounds.getNorthEast().lat > maxBounds.getNorthEast().lat ) {
maxBounds._northEast['lat'] = myBounds._northEast.lat;
}
if( (typeof maxBounds.getNorthEast().lng == 'undefined') || myBounds.getNorthEast().lng > maxBounds.getNorthEast().lng ) {
maxBounds._northEast['lng'] = myBounds._northEast.lng;
}
if( showOnly || (loaded >= urls.length) ) { // last one, or move after loop
mymap.fitBounds( maxBounds );
}
Ops, forgot url :D
Thanks a lot @ibrierley !! I need to dig into this. But I am no Java expert, this might take some time. (I am learning) But this looks very good! =) Cheers!
I think, I am going to rewrite my code completely. I like your approach much better and cleaner:
window.onload = function() {
var clickTimer = null;
var timer = 0;
var delay = 1000;
var prevent = false;
var maxBounds = L.bounds();//{ _southWest: {}, _northEast: {}};
var urls = [
{ url: 'Amberley%20Burgh.gpx', color: '#0040ff' },
{ url: 'Lancing%20SouthDownsWay.gpx', color: '#ff0000' },
{ url: 'MileOak%20Truleight%2012k.gpx', color: 'darkblue' },
{ url: 'MileOakCarPk%20Southdowns%20West.gpx', color: 'darkslateblue' },
{ url: 'Woodingdean%20Castle%20Hill.gpx', color: 'maroon' },
This is very good! Thanks again!
Cool, if it's useful I may put it on a Git repo sometime, then people can improve.
@ibrierley with your suggestions, you made me change the whole code for the better. Take a look at this: https://github.com/mjut/trailmap It is still a draft, but I am going to work more on this. Cheers-
You're welcome, it looks like you've grasped it all (there's not that much really), but any questions, feel free to ping me at ians.coding at gmail.com.
@ibrierley A slightly easier way to accomplish this is to use LatLngBounds.extend(...)
:
var maxBounds = L.latLngBounds();
tracks.forEach(function(track) {
maxBounds.extend(track.getBounds());
});
map.fitBounds(maxBounds);
Ah thank you!
I have multiple gpx-tracks loaded into a map. I place all of them (in a for each loop) with this:
gpxTrack.addTo(map);
It is working as expected. Now, I want to center the map to show all the tracks. I know, there is this:
But this is not working, as I have multiple gpx-tracks.
My code looks like this:
This is two tracks loading into the map. But the position and zoom of the map is hard coded. My aim is to automatically zoom and position the map to the tracks.
Any suggestions or hints that lead me into the right direction?
Thanks a lot for your help,
Stephan