Skip to content

Commit

Permalink
Center scroll wheel zoom on mouse pointer (#1191)
Browse files Browse the repository at this point in the history
* Center scroll wheel zoom on mouse pointer.

* Formatting fixes.

* Remove unused import.

* Resolve Formatting Issues

* Fix Issue From Formatting

Co-authored-by: Luka S <58115698+JaffaKetchup@users.noreply.github.com>
Co-authored-by: Luka S <lukastilling@gmail.com>
  • Loading branch information
3 people authored Mar 20, 2022
1 parent 17c1298 commit aec8274
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions lib/src/gestures/gestures.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';
import 'dart:math' as math;
import 'dart:math';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
Expand All @@ -27,19 +26,25 @@ abstract class MapGestureMixin extends State<FlutterMap>
void onPointerSignal(PointerSignalEvent pointerSignal) {
// Handle mouse scroll events if the enableScrollWheel parameter is enabled
if (pointerSignal is PointerScrollEvent &&
mapState.options.enableScrollWheel) {
if (pointerSignal.scrollDelta.dy != 0) {
// Check whether the mouse is scrolled down and calculate new zoom level
final delta = pointerSignal.scrollDelta.dy * (-0.01);
final zoom = delta > 0
? min(mapState.options.maxZoom ?? double.infinity,
mapState.zoom + delta)
: max(mapState.options.minZoom ?? 0, mapState.zoom + delta);
final newZoom = mapState.fitZoomToBounds(zoom);
// Move the map to the new zoom level
mapState.move(mapState.center, newZoom,
source: MapEventSource.scrollWheel);
}
mapState.options.enableScrollWheel &&
pointerSignal.scrollDelta.dy != 0) {
// Calculate new zoom level
final minZoom = mapState.options.minZoom ?? 0.0;
final maxZoom = mapState.options.maxZoom ?? double.infinity;
final newZoom = (mapState.zoom + pointerSignal.scrollDelta.dy * -0.005)
.clamp(minZoom, maxZoom);
// Calculate offset of mouse cursor from viewport center
final cursorPos = CustomPoint(
pointerSignal.localPosition.dx, pointerSignal.localPosition.dy);
final viewCenter = mapState.originalSize! / 2;
final offset = (cursorPos - viewCenter).rotate(mapState.rotationRad);
// Match new center coordinate to mouse cursor position
final scale = mapState.getZoomScale(newZoom, mapState.zoom);
final newOffset = offset * (1.0 - 1.0 / scale);
final mapCenter = mapState.project(mapState.center);
final newCenter = mapState.unproject(mapCenter + newOffset);
// Move to new center and zoom level
mapState.move(newCenter, newZoom, source: MapEventSource.scrollWheel);
}
}

Expand Down

1 comment on commit aec8274

@1Map
Copy link

@1Map 1Map commented on aec8274 May 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am new to Flutter. My gestures.dart in flutter pub-cache folder still shows the old code (left). How would I go about to update my flutter_map to use the new commited code from github (right) ?

Please sign in to comment.