leafletmapbox

Zoom to fit all markers in Mapbox or Leaflet


How do I set view to see all markers on map in Mapbox or Leaflet? Like Google Maps API does with bounds?

E.g:

var latlngbounds = new google.maps.LatLngBounds();
for (var i = 0; i < latlng.length; i++) {
  latlngbounds.extend(latlng[i]);
}
map.fitBounds(latlngbounds);

Solution

  • var group = new L.featureGroup([marker1, marker2, marker3]);
    
    map.fitBounds(group.getBounds());
    

    See the documentation for more info.