Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass click details to feature tapped #798

Merged
merged 3 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.15.0, Unreleased
### Breaking changes
* callbacks added to onFeatureTapped will now also get the position (`Point<double>`) and the location (`LatLng`) of the click passed when called [#798](https://github.com/flutter-mapbox-gl/maps/pull/798)


## 0.14.0, November 13, 2021
* Remove memory leaks by disposing internal components [#706](https://github.com/tobrun/flutter-mapbox-gl/pull/706)
* Improved annotation click order [#748](https://github.com/tobrun/flutter-mapbox-gl/pull/748)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1300,16 +1300,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 @@ -45,7 +45,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 @@ -828,13 +828,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 mapbox_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 OnAttributionClickCallback();
Expand Down Expand Up @@ -86,8 +90,10 @@ class MapboxMapController 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 @@ -181,8 +187,7 @@ class MapboxMapController 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 @@ -24,7 +24,7 @@ abstract class MapboxGlPlatform {

final onFillTappedPlatform = ArgumentCallbacks<String>();

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

final onCameraMoveStartedPlatform = ArgumentCallbacks<void>();

Expand Down
14 changes: 10 additions & 4 deletions mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
}
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 mapbox_gl_web/lib/src/mapbox_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,15 @@ class MapboxMapController extends MapboxGlPlatform
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