'Leaflet - zoom or pan to a GeoJSON layer when toggling on

Is there a function on Leaflet to zoom or pan to a GeoJSON layer when toggling on it?

var baseLayers = {
    "OpenStreetMap": osm
};

var map = L.map('map', {
    center: [13.751330328, 100.489664708],
    zoom: 10
});

var point = L.geoJson(null);

$.getJSON('example.php', function(data) {
    point.addData(data).addTo(map);
});

var overlays = {
    "Layer": point
};

L.control.layers(baseLayers, overlays).addTo(map);


Solution 1:[1]

You can add a add listener on the geoJSON layer or you add an overlayadd listener to the map.

point.on('add',()=>{
  map.fitBounds(point.getBounds())
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Falke Design