From 82f49b8a45721b966dcbea5880088d06e4762060 Mon Sep 17 00:00:00 2001 From: m0nac0 <58807793+m0nac0@users.noreply.github.com> Date: Thu, 19 May 2022 17:50:07 +0200 Subject: [PATCH] Cherry-pick upstream#798 (Pass click details to feature tapped) (#85) * Cherry-pick upstream#798 (Pass click details to feature tapped) https: //github.com/flutter-mapbox-gl/maps/pull/798 Co-Authored-By: Felix Horvat <24698503+felix-ht@users.noreply.github.com> --- .../com/mapbox/mapboxgl/MapboxMapController.java | 13 ++++++------- example/lib/layer.dart | 2 +- ios/Classes/MapboxMapController.swift | 8 ++++++-- lib/src/controller.dart | 13 +++++++++---- .../lib/src/mapbox_gl_platform_interface.dart | 2 +- .../lib/src/method_channel_mapbox_gl.dart | 14 ++++++++++---- maplibre_gl_web/lib/src/mapbox_map_controller.dart | 12 +++++++----- 7 files changed, 40 insertions(+), 24 deletions(-) diff --git a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java index 30a6ed3c0..a7cb05500 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java +++ b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java @@ -1298,16 +1298,15 @@ public boolean onMapClick(@NonNull LatLng point) { pointf.y + 10 ); Feature feature = firstFeatureOnLayers(rectF); + final Map arguments = new HashMap<>(); + arguments.put("x", pointf.x); + arguments.put("y", pointf.y); + arguments.put("lng", point.getLongitude()); + arguments.put("lat", point.getLatitude()); if(feature != null){ - final Map arguments = new HashMap<>(1); - arguments.put("featureId", feature.id()); + arguments.put("id", feature.id()); methodChannel.invokeMethod("feature#onTap", arguments); } else { - final Map arguments = new HashMap<>(5); - arguments.put("x", pointf.x); - arguments.put("y", pointf.y); - arguments.put("lng", point.getLongitude()); - arguments.put("lat", point.getLatitude()); methodChannel.invokeMethod("map#onMapClick", arguments); } return true; diff --git a/example/lib/layer.dart b/example/lib/layer.dart index 9de610e82..0aa8ac309 100644 --- a/example/lib/layer.dart +++ b/example/lib/layer.dart @@ -43,7 +43,7 @@ class LayerState extends State { controller.onFeatureTapped.add(onFeatureTap); } - void onFeatureTap(dynamic featureId) { + void onFeatureTap(dynamic featureId, Point point, LatLng latLng) { final snackBar = SnackBar( content: Text( 'Tapped feature with id $featureId', diff --git a/ios/Classes/MapboxMapController.swift b/ios/Classes/MapboxMapController.swift index 3210da910..5c87ff7c5 100644 --- a/ios/Classes/MapboxMapController.swift +++ b/ios/Classes/MapboxMapController.swift @@ -817,13 +817,17 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma @objc @IBAction func handleMapTap(sender: UITapGestureRecognizer) { // Get the CGPoint where the user tapped. let point = sender.location(in: mapView) + let coordinate = mapView.convert(point, toCoordinateFrom: mapView) if let feature = firstFeatureOnLayers(at: point), let id = feature.identifier { channel?.invokeMethod("feature#onTap", arguments: [ - "featureId": id + "id": id, + "x": point.x, + "y": point.y, + "lng": coordinate.longitude, + "lat": coordinate.latitude, ]) } else { - let coordinate = mapView.convert(point, toCoordinateFrom: mapView) channel?.invokeMethod("map#onMapClick", arguments: [ "x": point.x, "y": point.y, diff --git a/lib/src/controller.dart b/lib/src/controller.dart index 37550effc..d0e3f7306 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -5,6 +5,10 @@ part of maplibre_gl; typedef void OnMapClickCallback(Point point, LatLng coordinates); + +typedef void OnFeatureTappedCallback( + dynamic id, Point point, LatLng coordinates); + typedef void OnMapLongClickCallback(Point point, LatLng coordinates); typedef void OnStyleLoadedCallback(); @@ -83,8 +87,10 @@ class MaplibreMapController extends ChangeNotifier { } }); - _mapboxGlPlatform.onFeatureTappedPlatform.add((featureId) { - onFeatureTapped(featureId); + _mapboxGlPlatform.onFeatureTappedPlatform.add((payload) { + for (final fun in List.from(onFeatureTapped)) { + fun(payload["id"], payload["point"], payload["latLng"]); + } }); _mapboxGlPlatform.onCameraMoveStartedPlatform.add((_) { @@ -171,8 +177,7 @@ class MaplibreMapController extends ChangeNotifier { final ArgumentCallbacks onFillTapped = ArgumentCallbacks(); /// Callbacks to receive tap events for features (geojson layer) placed on this map. - final ArgumentCallbacks onFeatureTapped = - ArgumentCallbacks(); + final onFeatureTapped = []; /// Callbacks to receive tap events for info windows on symbols final ArgumentCallbacks onInfoWindowTapped = diff --git a/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart b/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart index 37af1a04f..ed781824b 100644 --- a/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart +++ b/maplibre_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart @@ -25,7 +25,7 @@ abstract class MapLibreGlPlatform { final onFillTappedPlatform = ArgumentCallbacks(); - final onFeatureTappedPlatform = ArgumentCallbacks(); + final onFeatureTappedPlatform = ArgumentCallbacks>(); final onCameraMoveStartedPlatform = ArgumentCallbacks(); diff --git a/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart b/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart index cd1922c6c..457aaf4cf 100644 --- a/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart +++ b/maplibre_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart @@ -36,10 +36,16 @@ class MethodChannelMaplibreGl extends MapLibreGlPlatform { } break; case 'feature#onTap': - final featureId = call.arguments['featureId']; - if (featureId != null) { - onFeatureTappedPlatform(featureId); - } + final id = call.arguments['id']; + final double x = call.arguments['x']; + final double y = call.arguments['y']; + final double lng = call.arguments['lng']; + final double lat = call.arguments['lat']; + onFeatureTappedPlatform({ + 'id': id, + 'point': Point(x, y), + 'latLng': LatLng(lat, lng) + }); break; case 'camera#onMoveStarted': onCameraMoveStartedPlatform(null); diff --git a/maplibre_gl_web/lib/src/mapbox_map_controller.dart b/maplibre_gl_web/lib/src/mapbox_map_controller.dart index 0d68e7f94..f0b514c37 100644 --- a/maplibre_gl_web/lib/src/mapbox_map_controller.dart +++ b/maplibre_gl_web/lib/src/mapbox_map_controller.dart @@ -455,13 +455,15 @@ class MaplibreMapController extends MapLibreGlPlatform void _onMapClick(Event e) { final features = _map.queryRenderedFeatures( [e.point.x, e.point.y], {"layers": _featureLayerIdentifiers.toList()}); + final payload = { + 'point': Point(e.point.x.toDouble(), e.point.y.toDouble()), + 'latLng': LatLng(e.lngLat.lat.toDouble(), e.lngLat.lng.toDouble()), + if (features.isNotEmpty) "id": features.first.id, + }; if (features.isNotEmpty) { - onFeatureTappedPlatform(features.first.id); + onFeatureTappedPlatform(payload); } else { - onMapClickPlatform({ - 'point': Point(e.point.x.toDouble(), e.point.y.toDouble()), - 'latLng': LatLng(e.lngLat.lat.toDouble(), e.lngLat.lng.toDouble()), - }); + onMapClickPlatform(payload); } }