Skip to content

Commit

Permalink
Cherry-pick upstream#798 (Pass click details to feature tapped) (#85)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
m0nac0 and felix-ht authored May 19, 2022
1 parent b8796ef commit 82f49b8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1298,16 +1298,15 @@ public boolean onMapClick(@NonNull LatLng point) {
pointf.y + 10
);
Feature feature = firstFeatureOnLayers(rectF);
final Map<String, Object> 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<String, Object> arguments = new HashMap<>(1);
arguments.put("featureId", feature.id());
arguments.put("id", feature.id());
methodChannel.invokeMethod("feature#onTap", arguments);
} else {
final Map<String, Object> 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;
Expand Down
2 changes: 1 addition & 1 deletion example/lib/layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class LayerState extends State {
controller.onFeatureTapped.add(onFeatureTap);
}

void onFeatureTap(dynamic featureId) {
void onFeatureTap(dynamic featureId, Point<double> point, LatLng latLng) {
final snackBar = SnackBar(
content: Text(
'Tapped feature with id $featureId',
Expand Down
8 changes: 6 additions & 2 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 9 additions & 4 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
part of maplibre_gl;

typedef void OnMapClickCallback(Point<double> point, LatLng coordinates);

typedef void OnFeatureTappedCallback(
dynamic id, Point<double> point, LatLng coordinates);

typedef void OnMapLongClickCallback(Point<double> point, LatLng coordinates);

typedef void OnStyleLoadedCallback();
Expand Down Expand Up @@ -83,8 +87,10 @@ class MaplibreMapController extends ChangeNotifier {
}
});

_mapboxGlPlatform.onFeatureTappedPlatform.add((featureId) {
onFeatureTapped(featureId);
_mapboxGlPlatform.onFeatureTappedPlatform.add((payload) {
for (final fun in List<OnFeatureTappedCallback>.from(onFeatureTapped)) {
fun(payload["id"], payload["point"], payload["latLng"]);
}
});

_mapboxGlPlatform.onCameraMoveStartedPlatform.add((_) {
Expand Down Expand Up @@ -171,8 +177,7 @@ class MaplibreMapController extends ChangeNotifier {
final ArgumentCallbacks<Fill> onFillTapped = ArgumentCallbacks<Fill>();

/// Callbacks to receive tap events for features (geojson layer) placed on this map.
final ArgumentCallbacks<dynamic> onFeatureTapped =
ArgumentCallbacks<dynamic>();
final onFeatureTapped = <OnFeatureTappedCallback>[];

/// Callbacks to receive tap events for info windows on symbols
final ArgumentCallbacks<Symbol> onInfoWindowTapped =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class MapLibreGlPlatform {

final onFillTappedPlatform = ArgumentCallbacks<String>();

final onFeatureTappedPlatform = ArgumentCallbacks<dynamic>();
final onFeatureTappedPlatform = ArgumentCallbacks<Map<String, dynamic>>();

final onCameraMoveStartedPlatform = ArgumentCallbacks<void>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(x, y),
'latLng': LatLng(lat, lng)
});
break;
case 'camera#onMoveStarted':
onCameraMoveStartedPlatform(null);
Expand Down
12 changes: 7 additions & 5 deletions maplibre_gl_web/lib/src/mapbox_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(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<double>(e.point.x.toDouble(), e.point.y.toDouble()),
'latLng': LatLng(e.lngLat.lat.toDouble(), e.lngLat.lng.toDouble()),
});
onMapClickPlatform(payload);
}
}

Expand Down

0 comments on commit 82f49b8

Please sign in to comment.