Skip to content

Commit 1215581

Browse files
committed
[google_maps_flutter] Add onPointOfInterestTap callback
Expose a place-ID-only POI tap stream across the federated plugin stack (Android, iOS, web) so apps can react when users tap built-in map POIs. Fixes flutter/flutter#60695
1 parent 7d17fc8 commit 1215581

100 files changed

Lines changed: 2390 additions & 153 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.19.0
2+
3+
* Adds support for tapping points of interest on the map.
4+
15
## 2.18.0
26

37
* Adds support for mapTypeControlEnabled, fullscreenControlEnabled, and streetViewControlEnabled on web.

‎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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ flutter:
3232
uses-material-design: true
3333
assets:
3434
- assets/
35+
# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
36+
# See https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changing-federated-plugins
37+
dependency_overrides:
38+
google_maps_flutter_android: {path: ../../../../packages/google_maps_flutter/google_maps_flutter_android}
39+
google_maps_flutter_ios: {path: ../../../../packages/google_maps_flutter/google_maps_flutter_ios}
40+
google_maps_flutter_platform_interface: {path: ../../../../packages/google_maps_flutter/google_maps_flutter_platform_interface}
41+
google_maps_flutter_web: {path: ../../../../packages/google_maps_flutter/google_maps_flutter_web}

‎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
@@ -59,6 +59,11 @@ class GoogleMapController {
5959
.listen((_) => _googleMapState.widget.onCameraIdle!()),
6060
);
6161
}
62+
_streamSubscriptions.add(
63+
GoogleMapsFlutterPlatform.instance
64+
.onPointOfInterestTap(mapId: mapId)
65+
.listen((PointOfInterestTapEvent e) => _googleMapState.onPointOfInterestTap(e.value)),
66+
);
6267
_streamSubscriptions.add(
6368
GoogleMapsFlutterPlatform.instance
6469
.onMarkerTap(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 platforms.
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,
@@ -703,6 +709,13 @@ class _GoogleMapState extends State<GoogleMap> {
703709
}
704710
}
705711

712+
void onPointOfInterestTap(PointOfInterestId pointOfInterestId) {
713+
final ArgumentCallback<PointOfInterestId>? onPointOfInterestTap = widget.onPointOfInterestTap;
714+
if (onPointOfInterestTap != null) {
715+
onPointOfInterestTap(pointOfInterestId);
716+
}
717+
}
718+
706719
void onTap(LatLng position) {
707720
final ArgumentCallback<LatLng>? onTap = widget.onTap;
708721
if (onTap != null) {

‎packages/google_maps_flutter/google_maps_flutter/pubspec.yaml‎

Lines changed: 12 additions & 5 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.0
5+
version: 2.19.0
66

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

2929
dev_dependencies:
3030
flutter_test:
@@ -41,3 +41,10 @@ topics:
4141
# The example deliberately includes limited-use secrets.
4242
false_secrets:
4343
- /example/web/index.html
44+
# FOR TESTING AND INITIAL REVIEW ONLY. DO NOT MERGE.
45+
# See https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changing-federated-plugins
46+
dependency_overrides:
47+
google_maps_flutter_android: {path: ../../../packages/google_maps_flutter/google_maps_flutter_android}
48+
google_maps_flutter_ios: {path: ../../../packages/google_maps_flutter/google_maps_flutter_ios}
49+
google_maps_flutter_platform_interface: {path: ../../../packages/google_maps_flutter/google_maps_flutter_platform_interface}
50+
google_maps_flutter_web: {path: ../../../packages/google_maps_flutter/google_maps_flutter_web}

‎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)