//Group1
m1=L.marker([7.11, -70.11]);
m2=L.marker([7.33, -70.33]);
m3=L.marker([7.55, -70.55]);
fg1=L.featureGroup([m1,m2,m3]);
//Group2
m4=L.marker([3.11, -75.11]);
m5=L.marker([3.33, -75.33]);
m6=L.marker([3.55, -75.55]);
fg2=L.featureGroup([m4,m5,m6]);
//BaseMap
baseLayer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
var map = L.map('map', {
center: [3, -70],
zoom: 4,
layers: [baseLayer, fg1, fg2]
});
//locate group 1
function LocateOne() {
LocateAllFeatures(map, fg1);
}
function LocateAll() {
LocateAllFeatures(map, [fg1,fg2]);
}
//Locate the features
function LocateAllFeatures(iobMap, iobFeatureGroup) {
if(Array.isArray(iobFeatureGroup)){
var obBounds = L.latLngBounds();
for (var i = 0; i < iobFeatureGroup.length; i++) {
obBounds.extend(iobFeatureGroup[i].getBounds());
}
iobMap.fitBounds(obBounds);
} else {
iobMap.fitBounds(iobFeatureGroup.getBounds());
}
}
.mymap{
height: 300px;
width: 100%;
}
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
<link href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" rel="stylesheet"/>
<div id="map" class="mymap"></div>
<button onclick="LocateOne()">locate group 1</button>
<button onclick="LocateAll()">locate All</button>