Skip to content

Commit 76dd2e9

Browse files
WudgeJalkclaude
andcommitted
fix: convert pinch scale to zoom through the CRS instead of log2
`_getZoomForScale` used `zoom + log2(scale)`, which only holds when each level doubles the scale. It now uses `crs.zoom(crs.scale(zoom) * scale)`, the formula of `MapCamera.getScaleZoom`, identical in EPSG:3857, so pinching x2 doubles the map scale in any CRS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 34457c1 commit 76dd2e9

2 files changed

Lines changed: 102 additions & 3 deletions

File tree

‎lib/src/gestures/map_interactive_viewer.dart‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,10 +1392,12 @@ class MapInteractiveViewerState extends State<MapInteractiveViewer>
13921392

13931393
// Utilities
13941394

1395+
// Through the CRS rather than `log2`: the ratio between zoom levels is not
1396+
// 2 in every CRS (e.g. `Proj4Crs` with custom resolutions).
13951397
double _getZoomForScale(double startZoom, double scale) {
1396-
final resultZoom =
1397-
scale == 1.0 ? startZoom : startZoom + math.log(scale) / math.ln2;
1398-
return _camera.clampZoom(resultZoom);
1398+
if (scale == 1.0) return _camera.clampZoom(startZoom);
1399+
final crs = _camera.crs;
1400+
return _camera.clampZoom(crs.zoom(crs.scale(startZoom) * scale));
13991401
}
14001402

14011403
Offset _rotateOffset(Offset offset) {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import 'package:flutter/widgets.dart';
2+
import 'package:flutter_map/flutter_map.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:latlong2/latlong.dart';
5+
import 'package:proj4dart/proj4dart.dart' as proj4;
6+
7+
// Resolutions with a ratio other than 2 between levels (the Swiss LV95 grid):
8+
// 2.5 -> 2 -> 1.5 -> 1 m/px from zoom 22.
9+
const _resolutions = <double>[
10+
4000, 3750, 3500, 3250, 3000, 2750, 2500, 2250, 2000, 1750, //
11+
1500, 1250, 1000, 750, 650, 500, 250, 100, 50, 20, //
12+
10, 5, 2.5, 2, 1.5, 1, 0.5, 0.25, 0.1,
13+
];
14+
15+
final _customCrs = Proj4Crs.fromFactory(
16+
code: 'TEST:CUSTOM',
17+
proj4Projection: proj4.Projection.get('EPSG:3857')!,
18+
resolutions: _resolutions,
19+
);
20+
21+
/// Two fingers around [c], [scale] times their initial 100 px apart.
22+
List<Offset> _fingers(Offset c, double scale) {
23+
final v = Offset(50 * scale, 0);
24+
return [c - v, c + v];
25+
}
26+
27+
Future<MapController> _pumpMap(
28+
WidgetTester tester, {
29+
required Crs crs,
30+
required double zoom,
31+
}) async {
32+
final controller = MapController();
33+
await tester.pumpWidget(
34+
Directionality(
35+
textDirection: TextDirection.ltr,
36+
child: Center(
37+
child: SizedBox(
38+
width: 400,
39+
height: 400,
40+
child: FlutterMap(
41+
mapController: controller,
42+
options: MapOptions(
43+
crs: crs,
44+
initialCenter: const LatLng(46.5, 6.6),
45+
initialZoom: zoom,
46+
minZoom: 8,
47+
maxZoom: 28,
48+
),
49+
children: const [],
50+
),
51+
),
52+
),
53+
),
54+
);
55+
return controller;
56+
}
57+
58+
/// Spreads two fingers through [scales].
59+
Future<void> _pinch(WidgetTester tester, List<double> scales) async {
60+
final c = tester.getCenter(find.byType(FlutterMap));
61+
final start = _fingers(c, 1);
62+
final g1 = await tester.startGesture(start[0], pointer: 1);
63+
final g2 = await tester.startGesture(start[1], pointer: 2);
64+
await tester.pump();
65+
for (final scale in scales) {
66+
final p = _fingers(c, scale);
67+
await g1.moveTo(p[0]);
68+
await g2.moveTo(p[1]);
69+
await tester.pump();
70+
}
71+
await g1.up();
72+
await g2.up();
73+
await tester.pumpAndSettle();
74+
// Let the double tap timer expire.
75+
await tester.pump(const Duration(seconds: 1));
76+
}
77+
78+
void main() {
79+
group('pinch zoom', () {
80+
testWidgets('doubles the scale when the ratio between levels is not 2',
81+
(tester) async {
82+
final controller = await _pumpMap(tester, crs: _customCrs, zoom: 22);
83+
await _pinch(tester, [1.5, 2]);
84+
expect(
85+
_customCrs.scale(controller.camera.zoom) / _customCrs.scale(22),
86+
closeTo(2, 1e-3),
87+
);
88+
});
89+
90+
testWidgets('adds one level in EPSG:3857', (tester) async {
91+
final controller =
92+
await _pumpMap(tester, crs: const Epsg3857(), zoom: 10);
93+
await _pinch(tester, [1.5, 2]);
94+
expect(controller.camera.zoom, closeTo(11, 1e-3));
95+
});
96+
});
97+
}

0 commit comments

Comments
 (0)