Center Leaflet Map
Sat Nov 16 2024 11:43:47 GMT+0000 (Coordinated Universal Time)
Saved by
@edcloudnineweb
// Calculate bounds
var bounds = new L.LatLngBounds();
locations.forEach(function(location) {
bounds.extend([location.lat, location.lng]);
});
// Set center to the midpoint of the bounds
var centerLatLng = bounds.getCenter();
var map = L.map('OSM-map', mapOptions).setView(centerLatLng, 1);
// Adjust the map to fit all markers within the bounds
map.fitBounds(bounds);
// Zoom out a bit more by decreasing the zoom level
map.setZoom(map.getZoom() - 1); // Decrease zoom level by 1
content_copyCOPY
Comments