I have a leaflet map with many layers and each layer has many objects. Is there a way to fit the map bounds so that all objects in all layers are visible?
Asked
Active
Viewed 1,409 times
2 Answers
4
If you're using or could switch to FeatureGroup, which is extended from LayerGroup, you have the getBounds method which would return the bounds of all the layers/features in your group. Those you could use with fitBounds method of your mapinstance.

iH8
- 27,722
- 4
- 67
- 76
-
1Thanks a lot. It only took a line of code after all! – T.G. Dallas Nov 16 '14 at 10:02
-
1No problem :) If this is the correct answer could you please accept it as such? [http://stackoverflow.com/help/accepted-answer](http://stackoverflow.com/help/accepted-answer) – iH8 Nov 16 '14 at 10:23
-
I came across another problem. What if a layer is empty? The featureGroup cannot be created and there are no bounds to fit... – T.G. Dallas Nov 18 '14 at 10:00
-
1That's kind of beyond the scope of this current question and hard to answer without some code and a testcase/plunkr. I'de suggest to open a new question, comments aren't fit for this. – iH8 Nov 18 '14 at 10:23
-
1Oh, my error, the layer, was not empty it was undefined. – T.G. Dallas Nov 18 '14 at 10:35
-1
map.addLayer("your layer name");