Skip to content

Commit

Permalink
Set default CameraFit maxZoom values to null
Browse files Browse the repository at this point in the history
The other parameters for CameraFit all have default values which will
not cause changes to the calculated CameraFit (i.e. padding is zero,
forceIntegerZoomLevel is false). Setting maxZoom to null makes it
consistent with the other parameters in that it will not affect the
calculated CameraFit. I chose null over double.infinity as in my opinion
the intent is clearer, no maximum.
  • Loading branch information
rorystephenson committed Jul 6, 2023
1 parent 150213e commit c327723
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions lib/src/map/camera/camera_fit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class CameraFit {
const factory CameraFit.bounds({
required LatLngBounds bounds,
EdgeInsets padding,
double maxZoom,
double? maxZoom,
bool forceIntegerZoomLevel,
}) = FitBounds._;

Expand All @@ -35,7 +35,7 @@ abstract class CameraFit {
const factory CameraFit.insideBounds({
required LatLngBounds bounds,
EdgeInsets padding,
double maxZoom,
double? maxZoom,
bool forceIntegerZoomLevel,
}) = FitInsideBounds._;

Expand All @@ -51,7 +51,7 @@ abstract class CameraFit {
const factory CameraFit.coordinates({
required List<LatLng> coordinates,
EdgeInsets padding,
double maxZoom,
double? maxZoom,
bool forceIntegerZoomLevel,
}) = FitCoordinates._;

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

/// Limits the maximum zoom level of the resulting fit.
/// Limits the maximum zoom level of the resulting fit if set.
///
/// Defaults to zoom level 18.
final double maxZoom;
/// Defaults to null.
final double? maxZoom;

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

Expand All @@ -95,7 +95,7 @@ class FitBounds extends CameraFit {
final paddingTotalXY = paddingTL + paddingBR;

var newZoom = _getBoundsZoom(camera, paddingTotalXY);
newZoom = math.min(maxZoom, newZoom);
if (maxZoom != null) newZoom = math.min(maxZoom!, newZoom);

final paddingOffset = (paddingBR - paddingTL) / 2;
final swPoint = camera.project(bounds.southWest, newZoom);
Expand Down Expand Up @@ -125,7 +125,10 @@ class FitBounds extends CameraFit {
CustomPoint<double> pixelPadding,
) {
final min = camera.minZoom ?? 0.0;
final max = camera.maxZoom ?? double.infinity;
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 @@ -167,10 +170,10 @@ class FitInsideBounds extends CameraFit {
/// Defaults to [EdgeInsets.zero].
final EdgeInsets padding;

/// Limits the maximum zoom level of the resulting fit.
/// Limits the maximum zoom level of the resulting fit if set.
///
/// Defaults to zoom level 18.
final double maxZoom;
/// Defaults to null.
final double? maxZoom;

/// Whether the zoom level of the resulting fit should be rounded to the
/// nearest integer level.
Expand All @@ -181,7 +184,7 @@ class FitInsideBounds extends CameraFit {
const FitInsideBounds._({
required this.bounds,
this.padding = EdgeInsets.zero,
this.maxZoom = 18,
this.maxZoom,
this.forceIntegerZoomLevel = false,
});

Expand Down Expand Up @@ -214,9 +217,9 @@ class FitInsideBounds extends CameraFit {
}

newZoom = math.max(
camera.minZoom ?? 0.0,
camera.minZoom ?? double.negativeInfinity,
math.min(
math.min(maxZoom, camera.maxZoom ?? double.infinity),
math.min(maxZoom ?? double.infinity, camera.maxZoom ?? double.infinity),
newZoom,
),
);
Expand Down Expand Up @@ -359,10 +362,10 @@ class FitCoordinates extends CameraFit {
/// Defaults to [EdgeInsets.zero].
final EdgeInsets padding;

/// Limits the maximum zoom level of the resulting fit.
/// Limits the maximum zoom level of the resulting fit if set.
///
/// Defaults to zoom level 18.
final double maxZoom;
/// Defaults to null.
final double? maxZoom;

/// Whether the zoom level of the resulting fit should be rounded to the
/// nearest integer level.
Expand All @@ -373,7 +376,7 @@ class FitCoordinates extends CameraFit {
const FitCoordinates._({
required this.coordinates,
this.padding = EdgeInsets.zero,
this.maxZoom = 18,
this.maxZoom = double.infinity,
this.forceIntegerZoomLevel = false,
});

Expand All @@ -386,7 +389,7 @@ class FitCoordinates extends CameraFit {
final paddingTotalXY = paddingTL + paddingBR;

var newZoom = _getCoordinatesZoom(camera, paddingTotalXY);
newZoom = math.min(maxZoom, newZoom);
if (maxZoom != null) newZoom = math.min(maxZoom!, newZoom);

final projectedPoints = [
for (final coord in coordinates) camera.project(coord, newZoom)
Expand Down Expand Up @@ -417,7 +420,10 @@ class FitCoordinates extends CameraFit {
CustomPoint<double> pixelPadding,
) {
final min = camera.minZoom ?? 0.0;
final max = camera.maxZoom ?? double.infinity;
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 Down

0 comments on commit c327723

Please sign in to comment.