From a9a4ec009b81b25ea72eabcc207b4e4a048aeb79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Silver=20Aabj=C3=B5e?= Date: Wed, 20 Mar 2024 15:12:37 +0200 Subject: [PATCH 1/2] +onLocationChanged --- example/lib/location.dart | 12 ++++++++---- ios/Classes/Generated/MapInterfaces.swift | 1 + ios/Classes/MapboxMapController.swift | 12 ++++++++++++ lib/src/callbacks.dart | 4 ++++ lib/src/events.dart | 10 ++++++++++ lib/src/map_widget.dart | 8 ++++++++ lib/src/mapbox_map.dart | 9 +++++++++ lib/src/mapbox_maps_platform.dart | 6 ++++++ lib/src/pigeons/map_interfaces.dart | 1 + 9 files changed, 59 insertions(+), 4 deletions(-) diff --git a/example/lib/location.dart b/example/lib/location.dart index d9dd2d72c..cd86c5aad 100644 --- a/example/lib/location.dart +++ b/example/lib/location.dart @@ -229,8 +229,8 @@ class LocationPageBodyState extends State { mapboxMap?.location.getSettings().then( (value) => ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text(""" - Location settings : - enabled : ${value.enabled}, + Location settings : + enabled : ${value.enabled}, puckBearingEnabled : ${value.puckBearingEnabled} puckBearing : ${value.puckBearing} pulsing : ${value.pulsingEnabled} @@ -250,8 +250,12 @@ class LocationPageBodyState extends State { @override Widget build(BuildContext context) { - final MapWidget mapWidget = - MapWidget(key: ValueKey("mapWidget"), onMapCreated: _onMapCreated); + final MapWidget mapWidget = MapWidget(key: ValueKey("mapWidget"), + onMapCreated: _onMapCreated, + onLocationChangeListener: (mapLocationChangeEventData) { + print(mapLocationChangeEventData); + } + ); final List listViewChildren = []; diff --git a/ios/Classes/Generated/MapInterfaces.swift b/ios/Classes/Generated/MapInterfaces.swift index d7a650581..3202b323a 100644 --- a/ios/Classes/Generated/MapInterfaces.swift +++ b/ios/Classes/Generated/MapInterfaces.swift @@ -341,6 +341,7 @@ enum _MapEvent: Int { case renderFrameStarted = 11 case renderFrameFinished = 12 case resourceRequest = 13 + case locationChange = 14 } /// The distance on each side between rectangles, when one is contained into other. diff --git a/ios/Classes/MapboxMapController.swift b/ios/Classes/MapboxMapController.swift index a8586242d..a489f25fe 100644 --- a/ios/Classes/MapboxMapController.swift +++ b/ios/Classes/MapboxMapController.swift @@ -168,6 +168,18 @@ class MapboxMapController: NSObject, FlutterPlatformView { mapboxMap.onResourceRequest.observe { [weak self] payload in self?.channel.invokeMethod(event.methodName, arguments: payload.toJSONString) }.store(in: &cancelables) + case .locationChange: + mapView.location.onLocationChange.observe { [weak self] payload in + var data = "" + do { + let jsonData = try JSONSerialization.data(withJSONObject: payload.first!.coordinate.toDict()) + data = String(data: jsonData, encoding: String.Encoding.utf8) ?? "" + } catch { + data = "" + } + + self?.channel.invokeMethod(event.methodName, arguments: data) + }.store(in: &cancelables) } } diff --git a/lib/src/callbacks.dart b/lib/src/callbacks.dart index 3b908f3e2..4339ab02d 100644 --- a/lib/src/callbacks.dart +++ b/lib/src/callbacks.dart @@ -111,3 +111,7 @@ typedef void OnMapLongTapListener(ScreenCoordinate coordinate); /// Gesture listener called on map scroll. typedef void OnMapScrollListener(ScreenCoordinate coordinate); + +/// Definition for listener invoked when the map location updates. +typedef void OnLocationChangeListener( + MapLocationChangeEventData mapLocationChangeEventData); diff --git a/lib/src/events.dart b/lib/src/events.dart index 141795f3f..7b3e82ee9 100644 --- a/lib/src/events.dart +++ b/lib/src/events.dart @@ -216,6 +216,16 @@ class ResourceEventData { cancelled = json['cancelled']; } +/// The class for map-loaded event in Observer +class MapLocationChangeEventData { + /// The `timeInterval.begin` represents the time when a style is set, and the + /// `timeInterval.end` is taken when the `map` is fully loaded. + final turf.Position position; + + MapLocationChangeEventData.fromJson(Map json) + : position = turf.Position(json['coordinates'][0], json["coordinates"][1]); +} + /// Describes data source of request for resource-request event. /// @param value String value of this enum enum DataSourceType { diff --git a/lib/src/map_widget.dart b/lib/src/map_widget.dart index 9e31ac3b7..c835ee039 100644 --- a/lib/src/map_widget.dart +++ b/lib/src/map_widget.dart @@ -67,6 +67,7 @@ class MapWidget extends StatefulWidget { this.onTapListener, this.onLongTapListener, this.onScrollListener, + this.onLocationChangeListener, }) : super(key: key) { if (onStyleLoadedListener != null) { _eventTypes.add(_MapEvent.styleLoaded); @@ -110,6 +111,9 @@ class MapWidget extends StatefulWidget { if (onResourceRequestListener != null) { _eventTypes.add(_MapEvent.resourceRequest); } + if (onLocationChangeListener != null) { + _eventTypes.add(_MapEvent.locationChange); + } } /// Describes the map options value when using a MapWidget. @@ -183,6 +187,9 @@ class MapWidget extends StatefulWidget { /// Invoked when map makes a request to load required resources. final OnResourceRequestListener? onResourceRequestListener; + /// Invoked when the Map's location has been updated. + final OnLocationChangeListener? onLocationChangeListener; + /// Which gestures should be consumed by the map. /// /// It is possible for other gesture recognizers to be competing with the map on pointer @@ -271,6 +278,7 @@ class _MapWidgetState extends State { onMapTapListener: widget.onTapListener, onMapLongTapListener: widget.onLongTapListener, onMapScrollListener: widget.onScrollListener, + onLocationChangeListener: widget.onLocationChangeListener ); _controller.complete(controller); if (widget.onMapCreated != null) { diff --git a/lib/src/mapbox_map.dart b/lib/src/mapbox_map.dart index d8b7842f1..933826f73 100644 --- a/lib/src/mapbox_map.dart +++ b/lib/src/mapbox_map.dart @@ -21,6 +21,7 @@ class MapboxMap extends ChangeNotifier { this.onMapTapListener, this.onMapLongTapListener, this.onMapScrollListener, + this.onLocationChangeListener, }) : _mapboxMapsPlatform = mapboxMapsPlatform { _proxyBinaryMessenger = _mapboxMapsPlatform.binaryMessenger; @@ -95,6 +96,11 @@ class MapboxMap extends ChangeNotifier { onResourceRequestListener?.call(argument); }); } + if (onLocationChangeListener != null) { + _mapboxMapsPlatform.onLocationChangePlatform.add((argument) { + onLocationChangeListener?.call(argument); + }); + } _setupGestures(); } @@ -148,6 +154,9 @@ class MapboxMap extends ChangeNotifier { /// Invoked when map makes a request to load required resources. final OnResourceRequestListener? onResourceRequestListener; + /// Invoked when the Map's location has been updated. + final OnLocationChangeListener? onLocationChangeListener; + /// The currently loaded Style]object. late StyleManager style = StyleManager(binaryMessenger: _proxyBinaryMessenger); diff --git a/lib/src/mapbox_maps_platform.dart b/lib/src/mapbox_maps_platform.dart index 11dd42724..b83a969de 100644 --- a/lib/src/mapbox_maps_platform.dart +++ b/lib/src/mapbox_maps_platform.dart @@ -26,6 +26,8 @@ class _MapboxMapsPlatform { final onStyleImageUnusedPlatform = ArgumentCallbacks(); final onResourceRequestPlatform = ArgumentCallbacks(); + final onLocationChangePlatform = + ArgumentCallbacks(); final int _channelSuffix = _suffixesRegistry.getSuffix(); late MethodChannel _channel; @@ -103,6 +105,10 @@ class _MapboxMapsPlatform { onResourceRequestPlatform( ResourceEventData.fromJson(jsonDecode(call.arguments))); break; + case _MapEvent.locationChange: + onLocationChangePlatform( + MapLocationChangeEventData.fromJson(jsonDecode(call.arguments))); + break; default: throw MissingPluginException(); } diff --git a/lib/src/pigeons/map_interfaces.dart b/lib/src/pigeons/map_interfaces.dart index 1c8d00b3d..1181482d9 100644 --- a/lib/src/pigeons/map_interfaces.dart +++ b/lib/src/pigeons/map_interfaces.dart @@ -386,6 +386,7 @@ enum _MapEvent { renderFrameStarted, renderFrameFinished, resourceRequest, + locationChange, } /// The distance on each side between rectangles, when one is contained into other. From 1c473e87e45edacaad1683390eb59f46cbd5bcb0 Mon Sep 17 00:00:00 2001 From: Skandar Munir Date: Thu, 25 Apr 2024 16:11:27 +0300 Subject: [PATCH 2/2] fixes on map single tap --- ios/Classes/GesturesController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ios/Classes/GesturesController.swift b/ios/Classes/GesturesController.swift index b847aed47..933286885 100644 --- a/ios/Classes/GesturesController.swift +++ b/ios/Classes/GesturesController.swift @@ -13,8 +13,8 @@ final class GesturesController: NSObject, GesturesSettingsInterface, UIGestureRe return } - let point = Point(mapView.mapboxMap.coordinate(for: gestureManager.singleTapGestureRecognizer.location(in: mapView))) - self.onGestureListener?.onTap(coordinate: ScreenCoordinate(x: point.coordinates.latitude, y: point.coordinates.longitude), completion: {_ in }) + let touchPoint = gestureManager.singleTapGestureRecognizer.location(in: mapView) + onGestureListener?.onTap(coordinate: touchPoint.toFLTScreenCoordinate(), completion: { _ in }) } func gestureManager(_ gestureManager: MapboxMaps.GestureManager, didEndAnimatingFor gestureType: MapboxMaps.GestureType) {}