Skip to content

Commit 84f9f8e

Browse files
authored
[google_maps_flutter] Add onPointOfInterestTap (#11872)
Adds `GoogleMap.onPointOfInterestTap`, a callback that fires when the user taps a built-in map point of interest. The callback receives a `PointOfInterestId` containing the place ID only, matching maintainer feedback on #4052 and #10963. The change is wired through the federated plugin stack: platform_interface (type + event stream), Android (`OnPoiClickListener`), iOS including sdk9/sdk10/shared (`didTapPOIWithPlaceID`), web (`IconMouseEvent.placeId` on map click), and the app-facing `google_maps_flutter` package. Tests cover Dart unit tests, Android Robolectric, iOS native, and web integration tests in the web example. Fixes flutter/flutter#60695 ## Pre-Review Checklist
1 parent 17e14c2 commit 84f9f8e

13 files changed

Lines changed: 110 additions & 8 deletions

File tree

‎packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.19.0
2+
3+
* Adds support for tapping points of interest on the map.
4+
On iOS, this requires `google_maps_flutter_ios_sdk9` or
5+
`google_maps_flutter_ios_sdk10` (the default `google_maps_flutter_ios`
6+
package does not receive new features).
7+
18
## 2.18.2
29

310
* Updates README to indicate that `google_maps_flutter_ios` is a legacy

‎packages/google_maps_flutter/google_maps_flutter/example/lib/map_click.dart‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class _MapClickBodyState extends State<_MapClickBody> {
3535
GoogleMapController? mapController;
3636
LatLng? _lastTap;
3737
LatLng? _lastLongPress;
38+
String? _lastPointOfInterestPlaceId;
3839

3940
@override
4041
Widget build(BuildContext context) {
@@ -51,6 +52,11 @@ class _MapClickBodyState extends State<_MapClickBody> {
5152
_lastLongPress = pos;
5253
});
5354
},
55+
onPointOfInterestTap: (PointOfInterestId pointOfInterestId) {
56+
setState(() {
57+
_lastPointOfInterestPlaceId = pointOfInterestId.value;
58+
});
59+
},
5460
);
5561

5662
final columnChildren = <Widget>[
@@ -73,6 +79,17 @@ class _MapClickBodyState extends State<_MapClickBody> {
7379
child: Text(_lastLongPress != null ? 'Long pressed' : '', textAlign: TextAlign.center),
7480
),
7581
);
82+
final lastPointOfInterestTap =
83+
'Point of interest place ID:\n${_lastPointOfInterestPlaceId ?? ""}';
84+
columnChildren.add(Center(child: Text(lastPointOfInterestTap, textAlign: TextAlign.center)));
85+
columnChildren.add(
86+
Center(
87+
child: Text(
88+
_lastPointOfInterestPlaceId != null ? 'Point of interest tapped' : '',
89+
textAlign: TextAlign.center,
90+
),
91+
),
92+
);
7693
}
7794
return Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: columnChildren);
7895
}

‎packages/google_maps_flutter/google_maps_flutter/example/pubspec.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ dependencies:
1818
# The example app is bundled with the plugin so we use a path dependency on
1919
# the parent directory to use the current plugin's version.
2020
path: ../
21-
google_maps_flutter_android: ^2.19.1
22-
google_maps_flutter_platform_interface: ^2.16.0
21+
google_maps_flutter_android: ^2.20.0
22+
google_maps_flutter_platform_interface: ^2.17.0
2323

2424
dev_dependencies:
2525
build_runner: ^2.1.10

‎packages/google_maps_flutter/google_maps_flutter/lib/google_maps_flutter.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export 'package:google_maps_flutter_platform_interface/google_maps_flutter_platf
5454
MinMaxZoomPreference,
5555
PatternItem,
5656
PinConfig,
57+
PointOfInterestId,
5758
Polygon,
5859
PolygonId,
5960
Polyline,

‎packages/google_maps_flutter/google_maps_flutter/lib/src/controller.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ class GoogleMapController {
111111
.onLongPress(mapId: mapId)
112112
.listen((MapLongPressEvent e) => _googleMapState.onLongPress(e.position)),
113113
);
114+
_streamSubscriptions.add(
115+
GoogleMapsFlutterPlatform.instance
116+
.onPointOfInterestTap(mapId: mapId)
117+
.listen((PointOfInterestTapEvent e) => _googleMapState.onPointOfInterestTap(e.value)),
118+
);
114119
_streamSubscriptions.add(
115120
GoogleMapsFlutterPlatform.instance
116121
.onClusterTap(mapId: mapId)

‎packages/google_maps_flutter/google_maps_flutter/lib/src/google_map.dart‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class GoogleMap extends StatefulWidget {
136136
this.onCameraIdle,
137137
this.onTap,
138138
this.onLongPress,
139+
this.onPointOfInterestTap,
139140
this.markerType = GoogleMapMarkerType.marker,
140141
this.colorScheme,
141142
String? mapId,
@@ -300,6 +301,11 @@ class GoogleMap extends StatefulWidget {
300301
/// Called every time a [GoogleMap] is long pressed.
301302
final ArgumentCallback<LatLng>? onLongPress;
302303

304+
/// Called when a point of interest on the map is tapped.
305+
///
306+
/// May not be supported on all implementations.
307+
final ArgumentCallback<PointOfInterestId>? onPointOfInterestTap;
308+
303309
/// True if a "My Location" layer should be shown on the map.
304310
///
305311
/// This layer includes a location indicator at the current device location,
@@ -717,6 +723,13 @@ class _GoogleMapState extends State<GoogleMap> {
717723
}
718724
}
719725

726+
void onPointOfInterestTap(PointOfInterestId pointOfInterestId) {
727+
final ArgumentCallback<PointOfInterestId>? onPointOfInterestTap = widget.onPointOfInterestTap;
728+
if (onPointOfInterestTap != null) {
729+
onPointOfInterestTap(pointOfInterestId);
730+
}
731+
}
732+
720733
void onClusterTap(Cluster cluster) {
721734
final ClusterManager? clusterManager = _clusterManagers[cluster.clusterManagerId];
722735
if (clusterManager == null) {

‎packages/google_maps_flutter/google_maps_flutter/pubspec.yaml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_maps_flutter
22
description: A Flutter plugin for integrating Google Maps in iOS and Android applications.
33
repository: https://github.com/flutter/packages/tree/main/packages/google_maps_flutter/google_maps_flutter
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
5-
version: 2.18.2
5+
version: 2.19.0
66

77
environment:
88
sdk: ^3.13.0
@@ -21,10 +21,10 @@ flutter:
2121
dependencies:
2222
flutter:
2323
sdk: flutter
24-
google_maps_flutter_android: ^2.19.1
24+
google_maps_flutter_android: ^2.20.0
2525
google_maps_flutter_ios: ^2.18.0
26-
google_maps_flutter_platform_interface: ^2.16.0
27-
google_maps_flutter_web: ^0.6.3
26+
google_maps_flutter_platform_interface: ^2.17.0
27+
google_maps_flutter_web: ^0.6.4
2828

2929
dev_dependencies:
3030
flutter_test:

‎packages/google_maps_flutter/google_maps_flutter/test/fake_google_maps_flutter_platform.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ class FakeGoogleMapsFlutterPlatform extends GoogleMapsFlutterPlatform {
227227
return mapEventStreamController.stream.whereType<CircleTapEvent>();
228228
}
229229

230+
@override
231+
Stream<PointOfInterestTapEvent> onPointOfInterestTap({required int mapId}) {
232+
return mapEventStreamController.stream.whereType<PointOfInterestTapEvent>();
233+
}
234+
230235
@override
231236
Stream<MapTapEvent> onTap({required int mapId}) {
232237
return mapEventStreamController.stream.whereType<MapTapEvent>();

‎packages/google_maps_flutter/google_maps_flutter/test/google_maps_flutter_export_test.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void main() {
5050
main_file.MinMaxZoomPreference;
5151
main_file.PatternItem;
5252
main_file.PinConfig;
53+
main_file.PointOfInterestId;
5354
main_file.Polygon;
5455
main_file.PolygonId;
5556
main_file.Polyline;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/widgets.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:google_maps_flutter/google_maps_flutter.dart';
8+
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
9+
10+
import 'fake_google_maps_flutter_platform.dart';
11+
12+
void main() {
13+
TestWidgetsFlutterBinding.ensureInitialized();
14+
late FakeGoogleMapsFlutterPlatform platform;
15+
16+
setUp(() {
17+
platform = FakeGoogleMapsFlutterPlatform();
18+
GoogleMapsFlutterPlatform.instance = platform;
19+
});
20+
21+
testWidgets('onPointOfInterestTap callback receives place ID from platform event', (
22+
WidgetTester tester,
23+
) async {
24+
PointOfInterestId? tappedPointOfInterestId;
25+
26+
await tester.pumpWidget(
27+
Directionality(
28+
textDirection: TextDirection.ltr,
29+
child: GoogleMap(
30+
initialCameraPosition: const CameraPosition(target: LatLng(0.0, 0.0)),
31+
onPointOfInterestTap: (PointOfInterestId pointOfInterestId) {
32+
tappedPointOfInterestId = pointOfInterestId;
33+
},
34+
),
35+
),
36+
);
37+
38+
await tester.pump();
39+
40+
expect(platform.createdIds, isNotEmpty);
41+
final int mapId = platform.createdIds.first;
42+
43+
platform.mapEventStreamController.add(
44+
PointOfInterestTapEvent(mapId, const PointOfInterestId('place-123')),
45+
);
46+
47+
await tester.pump();
48+
49+
expect(tappedPointOfInterestId, const PointOfInterestId('place-123'));
50+
});
51+
}

0 commit comments

Comments
 (0)