Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter to fit to integer zoom levels #1367

Merged
merged 3 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/flutter_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,17 @@ class FitBoundsOptions {
final double? zoom;
final bool inside;

/// By default the calculation will work with fractional zoom levels.
/// If this parameter is set to [true] fractional zoom levels will be avoided.
/// Instead they will be round to the next suitable integer.
final bool forceIntegerZoomLevel;

const FitBoundsOptions({
this.padding = EdgeInsets.zero,
this.maxZoom = 17.0,
this.zoom,
this.inside = false,
this.forceIntegerZoomLevel = false,
});
}

Expand Down
13 changes: 11 additions & 2 deletions lib/src/map/flutter_map_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,12 @@ class FlutterMapState extends MapGestureMixin

final paddingTotalXY = paddingTL + paddingBR;

var zoom = getBoundsZoom(bounds, paddingTotalXY, inside: options.inside);
var zoom = getBoundsZoom(
bounds,
paddingTotalXY,
inside: options.inside,
forceIntegerZoomLevel: options.forceIntegerZoomLevel,
);
zoom = math.min(options.maxZoom, zoom);

final paddingOffset = (paddingBR - paddingTL) / 2;
Expand All @@ -528,7 +533,7 @@ class FlutterMapState extends MapGestureMixin
}

double getBoundsZoom(LatLngBounds bounds, CustomPoint<double> padding,
{bool inside = false}) {
{bool inside = false, bool forceIntegerZoomLevel = false}) {
var zoom = this.zoom;
final min = options.minZoom ?? 0.0;
final max = options.maxZoom ?? double.infinity;
Expand All @@ -544,6 +549,10 @@ class FlutterMapState extends MapGestureMixin

zoom = getScaleZoom(scale, zoom);

if (forceIntegerZoomLevel) {
zoom = inside ? zoom.ceilToDouble() : zoom.floorToDouble();
}

return math.max(min, math.min(max, zoom));
}

Expand Down