Skip to content

Commit

Permalink
Pinchzoom regarding gesture center (#1081)
Browse files Browse the repository at this point in the history
* fixed zoom to zoom in regarding pinchzoom gesture and not map center

* removed unnecessary changes

* dart format
  • Loading branch information
Zzerr0r authored Feb 20, 2022
1 parent e8e389d commit 0b5b804
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/src/gestures/gestures.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ abstract class MapGestureMixin extends State<FlutterMap>
late LatLng _mapCenterStart;
late double _mapZoomStart;
late Offset _focalStartLocal;
late LatLng _focalStartLatLng;

late final AnimationController _flingController;
late Animation<Offset> _flingAnimation;
Expand Down Expand Up @@ -246,6 +247,7 @@ abstract class MapGestureMixin extends State<FlutterMap>
_mapZoomStart = mapState.zoom;
_mapCenterStart = mapState.center;
_focalStartLocal = _lastFocalLocal = details.localFocalPoint;
_focalStartLatLng = _offsetToCrs(_focalStartLocal);

_dragStarted = false;
_pinchZoomStarted = false;
Expand Down Expand Up @@ -398,11 +400,16 @@ abstract class MapGestureMixin extends State<FlutterMap>

if (_pinchMoveStarted) {
final oldCenterPt = mapState.project(mapState.center, newZoom);
final localDistanceOffset =
_rotateOffset(_lastFocalLocal - focalOffset);

final newCenterPt =
oldCenterPt + _offsetToPoint(localDistanceOffset);
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 +
zoomDifference +
_offsetToPoint(moveDifference);
newCenter = mapState.unproject(newCenterPt, newZoom);
} else {
newCenter = mapState.center;
Expand Down Expand Up @@ -572,13 +579,14 @@ abstract class MapGestureMixin extends State<FlutterMap>
);
}

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) {
Expand Down

0 comments on commit 0b5b804

Please sign in to comment.