Skip to content

Commit

Permalink
Add minZoom property to CameraFits (#1562)
Browse files Browse the repository at this point in the history
Use 0 as default value since negative zoom values are very unusual
  • Loading branch information
Robbendebiene authored Jul 7, 2023
1 parent 2c60d63 commit a48588b
Showing 1 changed file with 62 additions and 21 deletions.
83 changes: 62 additions & 21 deletions lib/src/map/camera/camera_fit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ abstract class CameraFit {
required LatLngBounds bounds,
EdgeInsets padding,
double? maxZoom,
double minZoom,
bool forceIntegerZoomLevel,
}) = FitBounds._;

Expand All @@ -36,6 +37,7 @@ abstract class CameraFit {
required LatLngBounds bounds,
EdgeInsets padding,
double? maxZoom,
double minZoom,
bool forceIntegerZoomLevel,
}) = FitInsideBounds._;

Expand All @@ -52,6 +54,7 @@ abstract class CameraFit {
required List<LatLng> coordinates,
EdgeInsets padding,
double? maxZoom,
double minZoom,
bool forceIntegerZoomLevel,
}) = FitCoordinates._;

Expand All @@ -68,11 +71,20 @@ class FitBounds extends CameraFit {
/// Defaults to [EdgeInsets.zero].
final EdgeInsets padding;

/// Limits the maximum zoom level of the resulting fit if set.
/// The inclusive upper zoom limit used for the resulting fit.
///
/// If the zoom level calculated for the fit exceeds the [maxZoom] value,
/// [maxZoom] will be used instead.
///
/// Defaults to null.
final double? maxZoom;

/// The inclusive lower zoom limit used for the resulting fit.
///
/// If the zoom level calculated for the fit undercuts the [minZoom] value,
/// [minZoom] will be used instead.
final double minZoom;

/// Whether the zoom level of the resulting fit should be rounded to the
/// nearest integer level.
///
Expand All @@ -83,6 +95,7 @@ class FitBounds extends CameraFit {
required this.bounds,
this.padding = EdgeInsets.zero,
this.maxZoom,
this.minZoom = 0,
this.forceIntegerZoomLevel = false,
});

Expand Down Expand Up @@ -124,11 +137,6 @@ class FitBounds extends CameraFit {
MapCamera camera,
CustomPoint<double> pixelPadding,
) {
final min = camera.minZoom ?? 0.0;
final max = math.min(
camera.maxZoom ?? double.infinity,
maxZoom ?? double.infinity,
);
final nw = bounds.northWest;
final se = bounds.southEast;
var size = camera.nonRotatedSize - pixelPadding;
Expand Down Expand Up @@ -157,7 +165,15 @@ class FitBounds extends CameraFit {
boundsZoom = boundsZoom.floorToDouble();
}

return math.max(min, math.min(max, boundsZoom));
final min = math.max(
camera.minZoom ?? 0,
minZoom,
);
final max = math.min(
camera.maxZoom ?? double.infinity,
maxZoom ?? double.infinity,
);
return boundsZoom.clamp(min, max);
}
}

Expand All @@ -170,11 +186,20 @@ class FitInsideBounds extends CameraFit {
/// Defaults to [EdgeInsets.zero].
final EdgeInsets padding;

/// Limits the maximum zoom level of the resulting fit if set.
/// The inclusive upper zoom limit used for the resulting fit.
///
/// If the zoom level calculated for the fit exceeds the [maxZoom] value,
/// [maxZoom] will be used instead.
///
/// Defaults to null.
final double? maxZoom;

/// The inclusive lower zoom limit used for the resulting fit.
///
/// If the zoom level calculated for the fit undercuts the [minZoom] value,
/// [minZoom] will be used instead.
final double minZoom;

/// Whether the zoom level of the resulting fit should be rounded to the
/// nearest integer level.
///
Expand All @@ -185,6 +210,7 @@ class FitInsideBounds extends CameraFit {
required this.bounds,
this.padding = EdgeInsets.zero,
this.maxZoom,
this.minZoom = 0,
this.forceIntegerZoomLevel = false,
});

Expand Down Expand Up @@ -216,13 +242,15 @@ class FitInsideBounds extends CameraFit {
newZoom = newZoom.ceilToDouble();
}

newZoom = math.max(
camera.minZoom ?? double.negativeInfinity,
math.min(
math.min(maxZoom ?? double.infinity, camera.maxZoom ?? double.infinity),
newZoom,
),
final min = math.max(
camera.minZoom ?? 0,
minZoom,
);
final max = math.min(
camera.maxZoom ?? double.infinity,
maxZoom ?? double.infinity,
);
newZoom = newZoom.clamp(min, max);

final newCenter = _getCenter(
camera,
Expand Down Expand Up @@ -362,11 +390,20 @@ class FitCoordinates extends CameraFit {
/// Defaults to [EdgeInsets.zero].
final EdgeInsets padding;

/// Limits the maximum zoom level of the resulting fit if set.
/// The inclusive upper zoom limit used for the resulting fit.
///
/// If the zoom level calculated for the fit exceeds the [maxZoom] value,
/// [maxZoom] will be used instead.
///
/// Defaults to null.
final double? maxZoom;

/// The inclusive lower zoom limit used for the resulting fit.
///
/// If the zoom level calculated for the fit undercuts the [minZoom] value,
/// [minZoom] will be used instead.
final double minZoom;

/// Whether the zoom level of the resulting fit should be rounded to the
/// nearest integer level.
///
Expand All @@ -377,6 +414,7 @@ class FitCoordinates extends CameraFit {
required this.coordinates,
this.padding = EdgeInsets.zero,
this.maxZoom = double.infinity,
this.minZoom = 0,
this.forceIntegerZoomLevel = false,
});

Expand Down Expand Up @@ -419,11 +457,6 @@ class FitCoordinates extends CameraFit {
MapCamera camera,
CustomPoint<double> pixelPadding,
) {
final min = camera.minZoom ?? 0.0;
final max = math.min(
camera.maxZoom ?? double.infinity,
maxZoom ?? double.infinity,
);
var size = camera.nonRotatedSize - pixelPadding;
// Prevent negative size which results in NaN zoom value later on in the calculation
size = CustomPoint(math.max(0, size.x), math.max(0, size.y));
Expand All @@ -447,6 +480,14 @@ class FitCoordinates extends CameraFit {
newZoom = newZoom.floorToDouble();
}

return math.max(min, math.min(max, newZoom));
final min = math.max(
camera.minZoom ?? 0,
minZoom,
);
final max = math.min(
camera.maxZoom ?? double.infinity,
maxZoom ?? double.infinity,
);
return newZoom.clamp(min, max);
}
}

0 comments on commit a48588b

Please sign in to comment.