From c13e023d87ea671cf6642694bead422c75ada377 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Wed, 30 Oct 2024 22:14:52 +0800 Subject: [PATCH] Replace implementation type with our type in map --- np_gps_map/lib/np_gps_map.dart | 1 - np_gps_map/lib/src/gps_map.dart | 2 +- np_gps_map/lib/src/interactive_map.dart | 1 - .../lib/src/interactive_map/google.dart | 13 +++---- np_gps_map/lib/src/interactive_map/osm.dart | 3 +- np_gps_map/lib/src/map_coord.dart | 9 ----- np_gps_map/lib/src/native/google_gps_map.dart | 4 +-- np_gps_map/lib/src/osm_gps_map.dart | 2 +- np_gps_map/lib/src/place_picker.dart | 1 - np_gps_map/lib/src/type.dart | 35 +++++++++++++++++-- 10 files changed, 41 insertions(+), 30 deletions(-) delete mode 100644 np_gps_map/lib/src/map_coord.dart diff --git a/np_gps_map/lib/np_gps_map.dart b/np_gps_map/lib/np_gps_map.dart index 24abb808..00f47d03 100644 --- a/np_gps_map/lib/np_gps_map.dart +++ b/np_gps_map/lib/np_gps_map.dart @@ -2,7 +2,6 @@ library np_gps_map; export 'src/gps_map.dart'; export 'src/interactive_map.dart'; -export 'src/map_coord.dart'; export 'src/place_picker.dart'; export 'src/type.dart'; export 'src/util.dart' show initGpsMap; diff --git a/np_gps_map/lib/src/gps_map.dart b/np_gps_map/lib/src/gps_map.dart index fa6f7feb..a53ab2bf 100644 --- a/np_gps_map/lib/src/gps_map.dart +++ b/np_gps_map/lib/src/gps_map.dart @@ -1,8 +1,8 @@ import 'package:flutter/material.dart'; -import 'package:np_gps_map/src/map_coord.dart'; import 'package:np_gps_map/src/native/google_gps_map.dart' if (dart.library.html) 'package:np_gps_map/src/web/google_gps_map.dart'; import 'package:np_gps_map/src/osm_gps_map.dart'; +import 'package:np_gps_map/src/type.dart'; import 'package:np_gps_map/src/util.dart'; import 'package:np_platform_util/np_platform_util.dart'; diff --git a/np_gps_map/lib/src/interactive_map.dart b/np_gps_map/lib/src/interactive_map.dart index ef6777d9..369175ec 100644 --- a/np_gps_map/lib/src/interactive_map.dart +++ b/np_gps_map/lib/src/interactive_map.dart @@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; import 'package:np_gps_map/src/gps_map.dart'; import 'package:np_gps_map/src/interactive_map/google.dart'; import 'package:np_gps_map/src/interactive_map/osm.dart'; -import 'package:np_gps_map/src/map_coord.dart'; import 'package:np_gps_map/src/type.dart'; import 'package:np_gps_map/src/util.dart'; import 'package:np_platform_util/np_platform_util.dart'; diff --git a/np_gps_map/lib/src/interactive_map/google.dart b/np_gps_map/lib/src/interactive_map/google.dart index 5235cf87..97d8d54a 100644 --- a/np_gps_map/lib/src/interactive_map/google.dart +++ b/np_gps_map/lib/src/interactive_map/google.dart @@ -3,10 +3,8 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:google_maps_cluster_manager/google_maps_cluster_manager.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; -import 'package:latlong2/latlong.dart' as type; import 'package:np_common/object_util.dart'; import 'package:np_gps_map/src/interactive_map.dart'; -import 'package:np_gps_map/src/map_coord.dart'; import 'package:np_gps_map/src/type.dart' as type; typedef GoogleClusterBuilder = FutureOr Function( @@ -28,7 +26,7 @@ class GoogleInteractiveMap extends StatefulWidget { @override State createState() => _GoogleInteractiveMapState(); - final MapCoord? initialPosition; + final type.MapCoord? initialPosition; final double? initialZoom; final List? dataPoints; final GoogleClusterBuilder? clusterBuilder; @@ -64,10 +62,7 @@ class _GoogleInteractiveMapState extends State { onCameraMove: (position) { _clusterManager.onCameraMove(position); widget.onCameraMove?.call(type.CameraPosition( - center: type.LatLng( - position.target.latitude, - position.target.longitude, - ), + center: position.target.toMapCoord(), zoom: position.zoom, rotation: position.bearing, )); @@ -134,7 +129,7 @@ class _ParentController implements InteractiveMapController { const _ParentController(this.controller); @override - void setPosition(MapCoord position) { + void setPosition(type.MapCoord position) { controller .animateCamera(CameraUpdate.newLatLngZoom(position.toLatLng(), 10)); } @@ -142,7 +137,7 @@ class _ParentController implements InteractiveMapController { final GoogleMapController controller; } -extension on MapCoord { +extension on type.MapCoord { LatLng toLatLng() => LatLng(latitude, longitude); } diff --git a/np_gps_map/lib/src/interactive_map/osm.dart b/np_gps_map/lib/src/interactive_map/osm.dart index 531d0297..2bc31c21 100644 --- a/np_gps_map/lib/src/interactive_map/osm.dart +++ b/np_gps_map/lib/src/interactive_map/osm.dart @@ -7,7 +7,6 @@ import 'package:flutter_map_marker_cluster/flutter_map_marker_cluster.dart'; import 'package:latlong2/latlong.dart'; import 'package:np_common/object_util.dart'; import 'package:np_gps_map/src/interactive_map.dart'; -import 'package:np_gps_map/src/map_coord.dart'; import 'package:np_gps_map/src/type.dart'; import 'package:rxdart/rxdart.dart'; @@ -51,7 +50,7 @@ class _OsmInteractiveMapState extends State { _subscriptions.add(_controller.mapEventStream.listen((ev) { _mapRotationRadSubject.add(ev.camera.rotationRad); widget.onCameraMove?.call(CameraPosition( - center: ev.camera.center, + center: ev.camera.center.toMapCoord(), zoom: ev.camera.zoom, rotation: (360 - ev.camera.rotation) % 360, )); diff --git a/np_gps_map/lib/src/map_coord.dart b/np_gps_map/lib/src/map_coord.dart deleted file mode 100644 index ca35a1df..00000000 --- a/np_gps_map/lib/src/map_coord.dart +++ /dev/null @@ -1,9 +0,0 @@ -class MapCoord { - const MapCoord(this.latitude, this.longitude); - - @override - String toString() => "MapCoord {latitude: $latitude, longitude: $longitude}"; - - final double latitude; - final double longitude; -} 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 67f4d872..0d1a2f25 100644 --- a/np_gps_map/lib/src/native/google_gps_map.dart +++ b/np_gps_map/lib/src/native/google_gps_map.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; -import 'package:np_gps_map/src/map_coord.dart'; +import 'package:np_gps_map/src/type.dart' as type; class GoogleGpsMap extends StatelessWidget { const GoogleGpsMap({ @@ -46,7 +46,7 @@ class GoogleGpsMap extends StatelessWidget { ); } - final MapCoord center; + final type.MapCoord center; final double zoom; 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 ba56f8a5..df74c340 100644 --- a/np_gps_map/lib/src/osm_gps_map.dart +++ b/np_gps_map/lib/src/osm_gps_map.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong2/latlong.dart'; -import 'package:np_gps_map/src/map_coord.dart'; +import 'package:np_gps_map/src/type.dart'; import 'package:url_launcher/url_launcher_string.dart'; class OsmGpsMap extends StatelessWidget { diff --git a/np_gps_map/lib/src/place_picker.dart b/np_gps_map/lib/src/place_picker.dart index c01f45c8..612efc95 100644 --- a/np_gps_map/lib/src/place_picker.dart +++ b/np_gps_map/lib/src/place_picker.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:np_gps_map/src/gps_map.dart'; import 'package:np_gps_map/src/interactive_map.dart'; -import 'package:np_gps_map/src/map_coord.dart'; import 'package:np_gps_map/src/type.dart'; class PlacePickerView extends StatelessWidget { diff --git a/np_gps_map/lib/src/type.dart b/np_gps_map/lib/src/type.dart index 168a5c5c..e6be7f4f 100644 --- a/np_gps_map/lib/src/type.dart +++ b/np_gps_map/lib/src/type.dart @@ -1,17 +1,46 @@ import 'package:equatable/equatable.dart'; +import 'package:google_maps_flutter/google_maps_flutter.dart' as gmap; import 'package:latlong2/latlong.dart'; import 'package:np_common/type.dart'; +/// A pair of latitude and longitude coordinates, stored as degrees +class MapCoord { + const MapCoord(this.latitude, this.longitude); + + MapCoord.fromJson(JsonObj json) + : latitude = json["lat"], + longitude = json["lng"]; + + @override + String toString() => "MapCoord {latitude: $latitude, longitude: $longitude}"; + + JsonObj toJson() => { + "lat": latitude, + "lng": longitude, + }; + + final double latitude; + final double longitude; +} + +extension GLatLngExtension on gmap.LatLng { + MapCoord toMapCoord() => MapCoord(latitude, longitude); +} + +extension LatLngExtension on LatLng { + MapCoord toMapCoord() => MapCoord(latitude, longitude); +} + class CameraPosition with EquatableMixin { const CameraPosition({ required this.center, required this.zoom, - required this.rotation, + this.rotation = 0, }); factory CameraPosition.fromJson(JsonObj json) { return CameraPosition( - center: LatLng.fromJson(json["center"]), + center: MapCoord.fromJson(json["center"]), zoom: json["zoom"], rotation: json["rotation"], ); @@ -35,7 +64,7 @@ class CameraPosition with EquatableMixin { @override List get props => [center, zoom, rotation]; - final LatLng center; + final MapCoord center; final double zoom; // The camera's bearing in degrees, measured clockwise from north. //