getBoundsWithMargin static method
LatLngBounds
getBoundsWithMargin(
- LatLngBounds visibleBounds,
- double marginInPixels,
- double zoomLevel
Implementation
static LatLngBounds getBoundsWithMargin(LatLngBounds visibleBounds, double marginInPixels, double zoomLevel) {
// Get current center latitude to use for calculation
double centerLatitude = (visibleBounds.northEast.latitude + visibleBounds.southWest.latitude) / 2;
// Calculate meters per pixel at the current zoom level and latitude
double metersPerPixelValue = metersPerPixel(zoomLevel, centerLatitude);
// Convert margin in pixels to meters
double marginInMeters = marginInPixels * metersPerPixelValue;
// Convert meters to latitude/longitude differences
double latMargin = metersToLatitude(marginInMeters);
double lngMargin = metersToLongitude(marginInMeters, centerLatitude);
// Adjust bounds with the margin
LatLng newNorthEast = LatLng(
visibleBounds.northEast.latitude + latMargin,
visibleBounds.northEast.longitude + lngMargin,
);
LatLng newSouthWest = LatLng(
visibleBounds.southWest.latitude - latMargin,
visibleBounds.southWest.longitude - lngMargin,
);
return LatLngBounds(newNorthEast, newSouthWest);
}