From f5fa9b558a796769f6148ce0aab0288fcbf5f8fd Mon Sep 17 00:00:00 2001 From: lukas gradl Date: Fri, 12 Nov 2021 16:09:58 +0100 Subject: [PATCH 1/3] fixed zoom to zoom in regarding pinchzoom gesture and not map center --- lib/src/gestures/gestures.dart | 58 +++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/lib/src/gestures/gestures.dart b/lib/src/gestures/gestures.dart index cdc8d2228..a6a875b4b 100644 --- a/lib/src/gestures/gestures.dart +++ b/lib/src/gestures/gestures.dart @@ -5,13 +5,17 @@ import 'dart:math'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/physics.dart'; -import 'package:flutter_map/flutter_map.dart'; -import 'package:flutter_map/src/gestures/interactive_flag.dart'; -import 'package:flutter_map/src/gestures/latlng_tween.dart'; -import 'package:flutter_map/src/map/map.dart'; +import '../../flutter_map.dart'; +import '../core/point.dart'; +import 'interactive_flag.dart'; +import 'latlng_tween.dart'; +import 'multi_finger_gesture.dart'; +import '../map/map.dart'; import 'package:latlong2/latlong.dart'; import 'package:positioned_tap_detector_2/positioned_tap_detector_2.dart'; +import 'map_events.dart'; + abstract class MapGestureMixin extends State with TickerProviderStateMixin { static const double _kMinFlingVelocity = 800.0; @@ -78,6 +82,7 @@ abstract class MapGestureMixin extends State late LatLng _mapCenterStart; late double _mapZoomStart; late Offset _focalStartLocal; + late LatLng _focalStartLatLng; late final AnimationController _flingController; late Animation _flingAnimation; @@ -103,9 +108,9 @@ abstract class MapGestureMixin extends State ..addListener(_handleFlingAnimation) ..addStatusListener(_flingAnimationStatusListener); _doubleTapController = - AnimationController(vsync: this, duration: Duration(milliseconds: 200)) - ..addListener(_handleDoubleTapZoomAnimation) - ..addStatusListener(_doubleTapZoomStatusListener); + AnimationController(vsync: this, duration: Duration(milliseconds: 200)) + ..addListener(_handleDoubleTapZoomAnimation) + ..addStatusListener(_doubleTapZoomStatusListener); } @override @@ -116,7 +121,7 @@ abstract class MapGestureMixin extends State final flags = options.interactiveFlags; final oldGestures = - _getMultiFingerGestureFlags(mapOptions: oldWidget.options); + _getMultiFingerGestureFlags(mapOptions: oldWidget.options); final gestures = _getMultiFingerGestureFlags(); if (flags != oldFlags || gestures != oldGestures) { @@ -257,6 +262,7 @@ abstract class MapGestureMixin extends State _mapZoomStart = mapState.zoom; _mapCenterStart = mapState.center; _focalStartLocal = _lastFocalLocal = details.localFocalPoint; + _focalStartLatLng = _offsetToCrs(_focalStartLocal); _dragStarted = false; _pinchZoomStarted = false; @@ -275,7 +281,7 @@ abstract class MapGestureMixin extends State } final eventSource = - _dragMode ? MapEventSource.onDrag : MapEventSource.onMultiFinger; + _dragMode ? MapEventSource.onDrag : MapEventSource.onMultiFinger; final flags = options.interactiveFlags; final focalOffset = details.localFocalPoint; @@ -314,11 +320,11 @@ abstract class MapGestureMixin extends State } } else { final hasIntPinchMove = - InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchMove); + InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchMove); final hasIntPinchZoom = - InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchZoom); + InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchZoom); final hasIntRotate = - InteractiveFlag.hasFlag(flags, InteractiveFlag.rotate); + InteractiveFlag.hasFlag(flags, InteractiveFlag.rotate); if (hasIntPinchMove || hasIntPinchZoom || hasIntRotate) { final hasGestureRace = options.enableMultiFingerGestureRace; @@ -326,7 +332,7 @@ abstract class MapGestureMixin extends State if (hasGestureRace && _gestureWinner == MultiFingerGesture.none) { if (hasIntPinchZoom && (_getZoomForScale(_mapZoomStart, details.scale) - _mapZoomStart) - .abs() >= + .abs() >= options.pinchZoomThreshold) { if (options.debugMultiFingerGestureWinner) { print('Multi Finger Gesture winner: Pinch Zoom'); @@ -356,7 +362,7 @@ abstract class MapGestureMixin extends State final hasGesturePinchZoom = MultiFingerGesture.hasFlag( gestures, MultiFingerGesture.pinchZoom); final hasGestureRotate = - MultiFingerGesture.hasFlag(gestures, MultiFingerGesture.rotate); + MultiFingerGesture.hasFlag(gestures, MultiFingerGesture.rotate); final hasMove = hasIntPinchMove && hasGesturePinchMove; final hasZoom = hasIntPinchZoom && hasGesturePinchZoom; @@ -409,11 +415,13 @@ abstract class MapGestureMixin extends State if (_pinchMoveStarted) { final oldCenterPt = mapState.project(mapState.center, newZoom); - final localDistanceOffset = - _rotateOffset(_lastFocalLocal - focalOffset); + final newFocalLatLong = _offsetToCrs(_focalStartLocal,newZoom); + final newFocalPt = mapState.project(newFocalLatLong, newZoom); + final oldFocalPt = mapState.project(_focalStartLatLng, newZoom); + final zoomDifference = oldFocalPt - newFocalPt; + final moveDifference = _rotateOffset(_focalStartLocal - _lastFocalLocal); - final newCenterPt = - oldCenterPt + _offsetToPoint(localDistanceOffset); + final newCenterPt = oldCenterPt + zoomDifference + _offsetToPoint(moveDifference); newCenter = mapState.unproject(newCenterPt, newZoom); } else { newCenter = mapState.center; @@ -476,7 +484,7 @@ abstract class MapGestureMixin extends State } final eventSource = - _dragMode ? MapEventSource.dragEnd : MapEventSource.multiFingerEnd; + _dragMode ? MapEventSource.dragEnd : MapEventSource.multiFingerEnd; if (_rotationStarted) { _rotationStarted = false; @@ -520,8 +528,8 @@ abstract class MapGestureMixin extends State var direction = details.velocity.pixelsPerSecond / magnitude; var distance = (Offset.zero & - Size(mapState.originalSize!.x as double, - mapState.originalSize!.y as double)) + Size(mapState.originalSize!.x as double, + mapState.originalSize!.y as double)) .shortestSide; var _flingOffset = _focalStartLocal - _lastFocalLocal; @@ -583,13 +591,13 @@ abstract class MapGestureMixin extends State ); } - LatLng _offsetToCrs(Offset offset) { - final focalStartPt = mapState.project(mapState.center, mapState.zoom); + LatLng _offsetToCrs(Offset offset, [double? zoom]) { + final focalStartPt = mapState.project(mapState.center, zoom ?? mapState.zoom); final point = (_offsetToPoint(offset) - (mapState.originalSize! / 2.0)) .rotate(mapState.rotationRad); var newCenterPt = focalStartPt + point; - return mapState.unproject(newCenterPt, mapState.zoom); + return mapState.unproject(newCenterPt, zoom ?? mapState.zoom); } void handleDoubleTap(TapPosition tapPosition) { @@ -753,7 +761,7 @@ abstract class MapGestureMixin extends State double _getZoomForScale(double startZoom, double scale) { var resultZoom = - scale == 1.0 ? startZoom : startZoom + math.log(scale) / math.ln2; + scale == 1.0 ? startZoom : startZoom + math.log(scale) / math.ln2; return mapState.fitZoomToBounds(resultZoom); } From 3c6b2a2323b25f84493f4337979b7d5738bb718d Mon Sep 17 00:00:00 2001 From: lukas gradl Date: Fri, 12 Nov 2021 16:27:14 +0100 Subject: [PATCH 2/3] removed unnecessary changes --- lib/src/gestures/gestures.dart | 46 ++++++++++++++++------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/src/gestures/gestures.dart b/lib/src/gestures/gestures.dart index a6a875b4b..69fb1c434 100644 --- a/lib/src/gestures/gestures.dart +++ b/lib/src/gestures/gestures.dart @@ -5,17 +5,13 @@ import 'dart:math'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/physics.dart'; -import '../../flutter_map.dart'; -import '../core/point.dart'; -import 'interactive_flag.dart'; -import 'latlng_tween.dart'; -import 'multi_finger_gesture.dart'; -import '../map/map.dart'; +import 'package:flutter_map/flutter_map.dart'; +import 'package:flutter_map/src/gestures/interactive_flag.dart'; +import 'package:flutter_map/src/gestures/latlng_tween.dart'; +import 'package:flutter_map/src/map/map.dart'; import 'package:latlong2/latlong.dart'; import 'package:positioned_tap_detector_2/positioned_tap_detector_2.dart'; -import 'map_events.dart'; - abstract class MapGestureMixin extends State with TickerProviderStateMixin { static const double _kMinFlingVelocity = 800.0; @@ -108,9 +104,9 @@ abstract class MapGestureMixin extends State ..addListener(_handleFlingAnimation) ..addStatusListener(_flingAnimationStatusListener); _doubleTapController = - AnimationController(vsync: this, duration: Duration(milliseconds: 200)) - ..addListener(_handleDoubleTapZoomAnimation) - ..addStatusListener(_doubleTapZoomStatusListener); + AnimationController(vsync: this, duration: Duration(milliseconds: 200)) + ..addListener(_handleDoubleTapZoomAnimation) + ..addStatusListener(_doubleTapZoomStatusListener); } @override @@ -121,7 +117,7 @@ abstract class MapGestureMixin extends State final flags = options.interactiveFlags; final oldGestures = - _getMultiFingerGestureFlags(mapOptions: oldWidget.options); + _getMultiFingerGestureFlags(mapOptions: oldWidget.options); final gestures = _getMultiFingerGestureFlags(); if (flags != oldFlags || gestures != oldGestures) { @@ -281,7 +277,7 @@ abstract class MapGestureMixin extends State } final eventSource = - _dragMode ? MapEventSource.onDrag : MapEventSource.onMultiFinger; + _dragMode ? MapEventSource.onDrag : MapEventSource.onMultiFinger; final flags = options.interactiveFlags; final focalOffset = details.localFocalPoint; @@ -320,11 +316,11 @@ abstract class MapGestureMixin extends State } } else { final hasIntPinchMove = - InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchMove); + InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchMove); final hasIntPinchZoom = - InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchZoom); + InteractiveFlag.hasFlag(flags, InteractiveFlag.pinchZoom); final hasIntRotate = - InteractiveFlag.hasFlag(flags, InteractiveFlag.rotate); + InteractiveFlag.hasFlag(flags, InteractiveFlag.rotate); if (hasIntPinchMove || hasIntPinchZoom || hasIntRotate) { final hasGestureRace = options.enableMultiFingerGestureRace; @@ -332,7 +328,7 @@ abstract class MapGestureMixin extends State if (hasGestureRace && _gestureWinner == MultiFingerGesture.none) { if (hasIntPinchZoom && (_getZoomForScale(_mapZoomStart, details.scale) - _mapZoomStart) - .abs() >= + .abs() >= options.pinchZoomThreshold) { if (options.debugMultiFingerGestureWinner) { print('Multi Finger Gesture winner: Pinch Zoom'); @@ -362,7 +358,7 @@ abstract class MapGestureMixin extends State final hasGesturePinchZoom = MultiFingerGesture.hasFlag( gestures, MultiFingerGesture.pinchZoom); final hasGestureRotate = - MultiFingerGesture.hasFlag(gestures, MultiFingerGesture.rotate); + MultiFingerGesture.hasFlag(gestures, MultiFingerGesture.rotate); final hasMove = hasIntPinchMove && hasGesturePinchMove; final hasZoom = hasIntPinchZoom && hasGesturePinchZoom; @@ -419,9 +415,11 @@ abstract class MapGestureMixin extends State final newFocalPt = mapState.project(newFocalLatLong, newZoom); final oldFocalPt = mapState.project(_focalStartLatLng, newZoom); final zoomDifference = oldFocalPt - newFocalPt; - final moveDifference = _rotateOffset(_focalStartLocal - _lastFocalLocal); + final moveDifference = + _rotateOffset(_focalStartLocal - _lastFocalLocal); - final newCenterPt = oldCenterPt + zoomDifference + _offsetToPoint(moveDifference); + final newCenterPt = oldCenterPt + + zoomDifference + _offsetToPoint(moveDifference); newCenter = mapState.unproject(newCenterPt, newZoom); } else { newCenter = mapState.center; @@ -484,7 +482,7 @@ abstract class MapGestureMixin extends State } final eventSource = - _dragMode ? MapEventSource.dragEnd : MapEventSource.multiFingerEnd; + _dragMode ? MapEventSource.dragEnd : MapEventSource.multiFingerEnd; if (_rotationStarted) { _rotationStarted = false; @@ -528,8 +526,8 @@ abstract class MapGestureMixin extends State var direction = details.velocity.pixelsPerSecond / magnitude; var distance = (Offset.zero & - Size(mapState.originalSize!.x as double, - mapState.originalSize!.y as double)) + Size(mapState.originalSize!.x as double, + mapState.originalSize!.y as double)) .shortestSide; var _flingOffset = _focalStartLocal - _lastFocalLocal; @@ -761,7 +759,7 @@ abstract class MapGestureMixin extends State double _getZoomForScale(double startZoom, double scale) { var resultZoom = - scale == 1.0 ? startZoom : startZoom + math.log(scale) / math.ln2; + scale == 1.0 ? startZoom : startZoom + math.log(scale) / math.ln2; return mapState.fitZoomToBounds(resultZoom); } From 9880cd2976a95c19be3a2d60576d909edc131f0b Mon Sep 17 00:00:00 2001 From: "lukas.gradl" Date: Sun, 20 Feb 2022 18:09:09 +0100 Subject: [PATCH 3/3] dart format --- lib/src/gestures/gestures.dart | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/src/gestures/gestures.dart b/lib/src/gestures/gestures.dart index 1369794cb..d66870cf0 100644 --- a/lib/src/gestures/gestures.dart +++ b/lib/src/gestures/gestures.dart @@ -400,15 +400,16 @@ abstract class MapGestureMixin extends State if (_pinchMoveStarted) { final oldCenterPt = mapState.project(mapState.center, newZoom); - final newFocalLatLong = _offsetToCrs(_focalStartLocal,newZoom); + final newFocalLatLong = _offsetToCrs(_focalStartLocal, newZoom); final newFocalPt = mapState.project(newFocalLatLong, newZoom); final oldFocalPt = mapState.project(_focalStartLatLng, newZoom); final zoomDifference = oldFocalPt - newFocalPt; final moveDifference = - _rotateOffset(_focalStartLocal - _lastFocalLocal); + _rotateOffset(_focalStartLocal - _lastFocalLocal); - final newCenterPt = oldCenterPt - + zoomDifference + _offsetToPoint(moveDifference); + final newCenterPt = oldCenterPt + + zoomDifference + + _offsetToPoint(moveDifference); newCenter = mapState.unproject(newCenterPt, newZoom); } else { newCenter = mapState.center; @@ -579,7 +580,8 @@ abstract class MapGestureMixin extends State } LatLng _offsetToCrs(Offset offset, [double? zoom]) { - final focalStartPt = mapState.project(mapState.center, zoom ?? mapState.zoom); + final focalStartPt = + mapState.project(mapState.center, zoom ?? mapState.zoom); final point = (_offsetToPoint(offset) - (mapState.originalSize! / 2.0)) .rotate(mapState.rotationRad);