diff --git a/example/lib/pages/multi_worlds.dart b/example/lib/pages/multi_worlds.dart index 3d40cc13f..8ef79549e 100644 --- a/example/lib/pages/multi_worlds.dart +++ b/example/lib/pages/multi_worlds.dart @@ -17,6 +17,24 @@ class MultiWorldsPage extends StatefulWidget { class _MultiWorldsPageState extends State { final LayerHitNotifier _hitNotifier = ValueNotifier(null); + final _customMarkers = []; + + Marker _buildPin(LatLng point) => Marker( + point: point, + width: 60, + height: 60, + child: GestureDetector( + onTap: () => ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text('Tapped existing marker'), + duration: Duration(seconds: 1), + showCloseIcon: true, + ), + ), + child: const Icon(Icons.location_pin, color: Colors.red), + ), + ); + @override Widget build(BuildContext context) { return Scaffold( @@ -25,10 +43,11 @@ class _MultiWorldsPageState extends State { body: Stack( children: [ FlutterMap( - options: const MapOptions( - initialCenter: LatLng(51.5, -0.09), + options: MapOptions( + initialCenter: const LatLng(51.5, -0.09), initialZoom: 0, initialRotation: 0, + onTap: (_, p) => setState(() => _customMarkers.add(_buildPin(p))), ), children: [ openStreetMapTileLayer, @@ -105,6 +124,7 @@ class _MultiWorldsPageState extends State { child: const Icon(Icons.backpack_outlined), ), ), + ..._customMarkers, ], ), ], diff --git a/lib/src/map/camera/camera.dart b/lib/src/map/camera/camera.dart index dcde25d59..5d046e8ec 100644 --- a/lib/src/map/camera/camera.dart +++ b/lib/src/map/camera/camera.dart @@ -240,12 +240,12 @@ class MapCamera { crs.offsetToLatLng(point, zoom ?? this.zoom); /// Returns the width of the world at the current zoom, or 0 if irrelevant. - double getWorldWidthAtZoom() { + double getWorldWidthAtZoom([double? zoom]) { if (!crs.replicatesWorldLongitude) { return 0; } - final offset0 = projectAtZoom(const LatLng(0, 0)); - final offset180 = projectAtZoom(const LatLng(0, 180)); + final offset0 = projectAtZoom(const LatLng(0, 0), zoom ?? this.zoom); + final offset180 = projectAtZoom(const LatLng(0, 180), zoom ?? this.zoom); return 2 * (offset180.dx - offset0.dx).abs(); } @@ -357,7 +357,17 @@ class MapCamera { (offset - nonRotatedSize.center(Offset.zero)).rotate(rotationRad); final newCenterPt = focalStartPt + point; - return unprojectAtZoom(newCenterPt, zoom ?? this.zoom); + final worldWidth = getWorldWidthAtZoom(zoom ?? this.zoom); + double bestX = newCenterPt.dx; + if (worldWidth != 0) { + while (bestX > worldWidth) { + bestX -= worldWidth; + } + while (bestX < 0) { + bestX += worldWidth; + } + } + return unprojectAtZoom(Offset(bestX, newCenterPt.dy), zoom ?? this.zoom); } /// Calculate the center point which would keep the same point of the map