I have a map with some clusters, but the clusters are cut when I'm using LatLngBounds to center the map. I need to add a padding to ensure that all the clusters are visible. I try using this answer. It's sometimes working, but not when the zoom is high and I don't understand why (in this case, the padding is really to large). I'm using flutter_map package.
Here what I have done (the mapBoundsEnlargePixels is random for now, i try to find the right value):
_latLngBounds = LatLngBounds.fromPoints(markers.map((e) => e.point).toList());
final mapBoundsEnlargePixels = 24;
const Crs crs = Epsg4326();
final CustomPoint<double> northEast =
crs.latLngToPoint(bounds.northEast, _mapController.zoom);
final CustomPoint<double> southWest =
crs.latLngToPoint(bounds.southWest, _mapController.zoom);
// north east
final northEastX = northEast.x + mapBoundsEnlargePixels;
final northEastY = northEast.y - mapBoundsEnlargePixels;
// south west
final southWestX = southWest.x - mapBoundsEnlargePixels;
final southWestY = southWest.y + mapBoundsEnlargePixels;
final LatLng? ne = crs.pointToLatLng(CustomPoint(northEastX, northEastY), _mapController.zoom);
final LatLng? sw = crs.pointToLatLng(CustomPoint(southWestX, southWestY), _mapController.zoom);
if (ne != null && sw != null) {
_latLngBounds!.extendBounds(LatLngBounds(ne, sw));
// Make small move on the map to fix tile not loaded when center to Bounds
print(_mapController.zoom);
_mapController
..fitBounds(bounds)
..move(_mapController.center, _mapController.zoom + 0.000001);
}
If you have an explanation, or even an other method that could be used, that'll be useful. Thanks
Latest version of flutter_map package allow you to set padding after setting bounds of map.
_mapController.fitCamera(CameraFit.bounds(bounds: bounds),padding: EdgeInsets.all(10));
you don't need to calculate zoom level.