How to fit to the bounds of multiple feature groups?

3,718 views
Skip to first unread message

Angus Munro

unread,
Apr 10, 2015, 11:13:22 AM4/10/15
to leafl...@googlegroups.com
I'm trying to fit my map to the largest bounds contained in several feature groups, however I'm having trouble getting the bounds of even one!

Here is my code;

map.fitBounds(group1); //Works fine

map
.fitBounds(group2); //Works fine but overrules group1



so I planned to simply compare them all and keep the largest values and then do:

var maxN = -1;
var maxE = -1;
var maxW = -1;
var maxS = -1;

maxN
= group1 .getBounds().min.x; //doesn't work
maxN
= group1 .getBounds().getNorth().lat; //doesn't work
maxN
= group1 .getBounds().__proto__.getNorth().lat; //doesn't work

//map.fitBounds(L.bounds(L.Point(maxN, maxW), L.Point(maxS, maxE)));

... but this is as far as I've gotten. I can't seem to use getBounds() of a feature group correctly. No matter what I try I can't get the lat/lngs to be returned. Can a more experienced user point me in the right direction? I'm having real trouble working my way to the variables from the leaflet documentation... I think I'm fundamentally misunderstanding something here.




Brendan Stone

unread,
Apr 10, 2015, 12:40:26 PM4/10/15
to leafl...@googlegroups.com
Use the extend method, try something like this:

var bounds = L.latLngBounds(group1.getBounds());
bounds
.extend(group2.getBounds());
bounds
.extend(group3.getBounds());

//etc


map
.fitBounds(bounds);



Regards,
Brendan
Message has been deleted

Angus Munro

unread,
Apr 13, 2015, 7:01:29 AM4/13/15
to leafl...@googlegroups.com
Hi Brendan,

I've getting an error:

Unable to get value of the property 'lat': object is null or undefined when I call getBounds() on any of my featuregroups.


   
var featureGroup1 = new L.FeatureGroup();
   
var featureGroup2 = new L.FeatureGroup();


   
..Code to create overlays, map and attatch featuregroups to map here


   
//Set the bounds
   
var addedBounds = false;


   
var pTL = L.point(60.337, 11.315);
   
var pBR = L.point(54.788, 3.493);
   
var bounds = L.bounds(pTL, pBR);


   
//Handle rendering group 1
   
if (!(typeof (group1GeoJSON) == 'undefined')) {
       
var count = 0;
        L
.geoJson(group1GeoJSON).eachLayer(function (layer) {
            layer
.addTo(featureGroup1);
       
});
   
        bounds
.extend(featureGroup1.getBounds());
        addedBounds
= true;
   
}


   
//Handle rendering group 2
   
if (!(typeof (group2GeoJSON) == 'undefined')) {
        L
.geoJson(group2GeoJSON).eachLayer(function (layer) {
            layer
.addTo(featureGroup2);
       
});
   
        bounds
.extend(featureGroup2.getBounds());
        addedBounds
= true;
   
}
   
   
if (addedBounds) {
            map
.fitBounds(bounds);
   
}

Iván Sánchez Ortega

unread,
Apr 16, 2015, 9:56:19 AM4/16/15
to leafl...@googlegroups.com


var superGroup = L.layerGroup();
supergroup
.addLayer(group1);
supergroup
.addLayer(group2);
map
.addLayer(superGroup);

// Do NOT add group1 nor group2 to the map!

map
.fitBounds(superGroup);
Reply all
Reply to author
Forward
0 new messages