From 55880944ceb73cda9e6d15a7150d1edb2918dce6 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Wed, 30 Oct 2024 22:22:49 +0800 Subject: [PATCH] Support rotation in static map --- app/lib/widget/viewer_detail_pane.dart | 3 +-- np_gps_map/lib/src/gps_map.dart | 13 ++++--------- np_gps_map/lib/src/native/google_gps_map.dart | 15 +++++++-------- np_gps_map/lib/src/osm_gps_map.dart | 16 ++++++++-------- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/app/lib/widget/viewer_detail_pane.dart b/app/lib/widget/viewer_detail_pane.dart index 4b509065..9248ef59 100644 --- a/app/lib/widget/viewer_detail_pane.dart +++ b/app/lib/widget/viewer_detail_pane.dart @@ -357,8 +357,7 @@ class _ViewerDetailPaneState extends State { stream: context.read().gpsMapProvider, builder: (context, gpsMapProvider) => GpsMap( providerHint: gpsMapProvider.requireData, - center: _gps!, - zoom: 16, + location: CameraPosition(center: _gps!, zoom: 16), onTap: _onMapTap, ), ), diff --git a/np_gps_map/lib/src/gps_map.dart b/np_gps_map/lib/src/gps_map.dart index a53ab2bf..1030c802 100644 --- a/np_gps_map/lib/src/gps_map.dart +++ b/np_gps_map/lib/src/gps_map.dart @@ -16,8 +16,7 @@ class GpsMap extends StatelessWidget { const GpsMap({ super.key, required this.providerHint, - required this.center, - required this.zoom, + required this.location, this.onTap, }); @@ -26,14 +25,12 @@ class GpsMap extends StatelessWidget { if (providerHint == GpsMapProvider.osm || (getRawPlatform() == NpPlatform.android && !isNewGMapsRenderer())) { return OsmGpsMap( - center: center, - zoom: zoom, + location: location, onTap: onTap, ); } else { return GoogleGpsMap( - center: center, - zoom: zoom, + location: location, onTap: onTap, ); } @@ -43,8 +40,6 @@ class GpsMap extends StatelessWidget { /// actual choice may be different depending on the runtime environment final GpsMapProvider providerHint; - /// A pair of latitude and longitude coordinates, stored as degrees - final MapCoord center; - final double zoom; + final CameraPosition location; final void Function()? onTap; } diff --git a/np_gps_map/lib/src/native/google_gps_map.dart b/np_gps_map/lib/src/native/google_gps_map.dart index 0d1a2f25..16abef2a 100644 --- a/np_gps_map/lib/src/native/google_gps_map.dart +++ b/np_gps_map/lib/src/native/google_gps_map.dart @@ -5,14 +5,13 @@ import 'package:np_gps_map/src/type.dart' as type; class GoogleGpsMap extends StatelessWidget { const GoogleGpsMap({ super.key, - required this.center, - required this.zoom, + required this.location, this.onTap, }); @override Widget build(BuildContext context) { - final centerLl = LatLng(center.latitude, center.longitude); + final center = LatLng(location.center.latitude, location.center.longitude); return GoogleMap( compassEnabled: false, mapToolbarEnabled: false, @@ -25,13 +24,14 @@ class GoogleGpsMap extends StatelessWidget { buildingsEnabled: false, // liteModeEnabled: true, initialCameraPosition: CameraPosition( - target: centerLl, - zoom: zoom, + target: center, + zoom: location.zoom, + bearing: location.rotation, ), markers: { Marker( markerId: const MarkerId("at"), - position: centerLl, + position: center, // for some reason, GoogleMap's onTap is not triggered if // tapped on top of the marker onTap: onTap, @@ -46,8 +46,7 @@ class GoogleGpsMap extends StatelessWidget { ); } - final type.MapCoord center; - final double zoom; + final type.CameraPosition location; final VoidCallback? onTap; } diff --git a/np_gps_map/lib/src/osm_gps_map.dart b/np_gps_map/lib/src/osm_gps_map.dart index c37c3e30..b2089e18 100644 --- a/np_gps_map/lib/src/osm_gps_map.dart +++ b/np_gps_map/lib/src/osm_gps_map.dart @@ -6,15 +6,14 @@ import 'package:np_gps_map/src/type.dart'; class OsmGpsMap extends StatelessWidget { const OsmGpsMap({ super.key, - required this.center, - required this.zoom, + required this.location, this.onTap, }); @override Widget build(BuildContext context) { const double pinSize = 48; - final centerLl = LatLng(center.latitude, center.longitude); + final center = LatLng(location.center.latitude, location.center.longitude); return GestureDetector( onTap: onTap, behavior: HitTestBehavior.opaque, @@ -23,8 +22,9 @@ class OsmGpsMap extends StatelessWidget { child: IgnorePointer( child: FlutterMap( options: MapOptions( - initialCenter: centerLl, - initialZoom: zoom, + initialCenter: center, + initialZoom: location.zoom, + initialRotation: (360 - location.rotation) % 360, interactionOptions: const InteractionOptions( flags: InteractiveFlag.none, ), @@ -34,11 +34,12 @@ class OsmGpsMap extends StatelessWidget { urlTemplate: "https://tile.openstreetmap.org/{z}/{x}/{y}.png", ), MarkerLayer( + rotate: true, markers: [ Marker( width: pinSize, height: pinSize, - point: centerLl, + point: center, alignment: Alignment.topCenter, child: const Image( image: AssetImage( @@ -56,7 +57,6 @@ class OsmGpsMap extends StatelessWidget { ); } - final MapCoord center; - final double zoom; + final CameraPosition location; final void Function()? onTap; }