Skip to content
New issue

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

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

Already on GitHub? # to your account

Pinchzoom regarding gesture center #1081

Merged
Merged
Changes from all commits
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
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