Skip to content

Commit d197dba

Browse files
fix: invoke map interaction callbacks with coordinates in primary world (#2025)
1 parent 433c660 commit d197dba

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

example/lib/pages/multi_worlds.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ class MultiWorldsPage extends StatefulWidget {
1717
class _MultiWorldsPageState extends State<MultiWorldsPage> {
1818
final LayerHitNotifier<String> _hitNotifier = ValueNotifier(null);
1919

20+
final _customMarkers = <Marker>[];
21+
22+
Marker _buildPin(LatLng point) => Marker(
23+
point: point,
24+
width: 60,
25+
height: 60,
26+
child: GestureDetector(
27+
onTap: () => ScaffoldMessenger.of(context).showSnackBar(
28+
const SnackBar(
29+
content: Text('Tapped existing marker'),
30+
duration: Duration(seconds: 1),
31+
showCloseIcon: true,
32+
),
33+
),
34+
child: const Icon(Icons.location_pin, color: Colors.red),
35+
),
36+
);
37+
2038
@override
2139
Widget build(BuildContext context) {
2240
return Scaffold(
@@ -25,10 +43,11 @@ class _MultiWorldsPageState extends State<MultiWorldsPage> {
2543
body: Stack(
2644
children: [
2745
FlutterMap(
28-
options: const MapOptions(
29-
initialCenter: LatLng(51.5, -0.09),
46+
options: MapOptions(
47+
initialCenter: const LatLng(51.5, -0.09),
3048
initialZoom: 0,
3149
initialRotation: 0,
50+
onTap: (_, p) => setState(() => _customMarkers.add(_buildPin(p))),
3251
),
3352
children: [
3453
openStreetMapTileLayer,
@@ -105,6 +124,7 @@ class _MultiWorldsPageState extends State<MultiWorldsPage> {
105124
child: const Icon(Icons.backpack_outlined),
106125
),
107126
),
127+
..._customMarkers,
108128
],
109129
),
110130
],

lib/src/map/camera/camera.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ class MapCamera {
240240
crs.offsetToLatLng(point, zoom ?? this.zoom);
241241

242242
/// Returns the width of the world at the current zoom, or 0 if irrelevant.
243-
double getWorldWidthAtZoom() {
243+
double getWorldWidthAtZoom([double? zoom]) {
244244
if (!crs.replicatesWorldLongitude) {
245245
return 0;
246246
}
247-
final offset0 = projectAtZoom(const LatLng(0, 0));
248-
final offset180 = projectAtZoom(const LatLng(0, 180));
247+
final offset0 = projectAtZoom(const LatLng(0, 0), zoom ?? this.zoom);
248+
final offset180 = projectAtZoom(const LatLng(0, 180), zoom ?? this.zoom);
249249
return 2 * (offset180.dx - offset0.dx).abs();
250250
}
251251

@@ -357,7 +357,17 @@ class MapCamera {
357357
(offset - nonRotatedSize.center(Offset.zero)).rotate(rotationRad);
358358

359359
final newCenterPt = focalStartPt + point;
360-
return unprojectAtZoom(newCenterPt, zoom ?? this.zoom);
360+
final worldWidth = getWorldWidthAtZoom(zoom ?? this.zoom);
361+
double bestX = newCenterPt.dx;
362+
if (worldWidth != 0) {
363+
while (bestX > worldWidth) {
364+
bestX -= worldWidth;
365+
}
366+
while (bestX < 0) {
367+
bestX += worldWidth;
368+
}
369+
}
370+
return unprojectAtZoom(Offset(bestX, newCenterPt.dy), zoom ?? this.zoom);
361371
}
362372

363373
/// Calculate the center point which would keep the same point of the map

0 commit comments

Comments
 (0)