From b2a516bdbc08cdea825b85e7d24dd7185d35eaad Mon Sep 17 00:00:00 2001 From: tenninebt <5684363+tenninebt@users.noreply.github.com> Date: Tue, 15 Sep 2026 22:56:06 +0200 Subject: [PATCH] [google_maps_flutter_web] Add onPointOfInterestTap support Detect placeId on map click events via a local JS interop extension and emit PointOfInterestTapEvent for the new platform interface stream. --- .../google_maps_flutter_web/CHANGELOG.md | 4 +++ .../example/3-64/pubspec.yaml | 2 +- .../google_maps_controller_test.dart | 30 +++++++++++++++++++ .../google_maps_plugin_test.dart | 7 +++++ .../example/latest/pubspec.yaml | 2 +- .../lib/google_maps_flutter_web.dart | 1 + .../lib/src/google_maps_controller.dart | 5 ++++ .../lib/src/google_maps_flutter_web.dart | 5 ++++ .../lib/src/map_mouse_event_extension.dart | 22 ++++++++++++++ .../google_maps_flutter_web/pubspec.yaml | 4 +-- 10 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 packages/google_maps_flutter/google_maps_flutter_web/lib/src/map_mouse_event_extension.dart diff --git a/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md b/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md index a65222c7bffd..a359e89a20e0 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md +++ b/packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.6.4 + +* Adds support for tapping points of interest on the map. + ## 0.6.3+1 * Fixes flickering of byte-backed advanced markers when updating their position on web. diff --git a/packages/google_maps_flutter/google_maps_flutter_web/example/3-64/pubspec.yaml b/packages/google_maps_flutter/google_maps_flutter_web/example/3-64/pubspec.yaml index 320ad0bf1c51..67402690c4c9 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/example/3-64/pubspec.yaml +++ b/packages/google_maps_flutter/google_maps_flutter_web/example/3-64/pubspec.yaml @@ -8,7 +8,7 @@ environment: dependencies: flutter: sdk: flutter - google_maps_flutter_platform_interface: ^2.16.0 + google_maps_flutter_platform_interface: ^2.17.0 google_maps_flutter_web: path: ../.. web: ^1.0.0 diff --git a/packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_controller_test.dart b/packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_controller_test.dart index 1efafae752fc..6008918c23d9 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_controller_test.dart +++ b/packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_controller_test.dart @@ -248,6 +248,36 @@ void main() { expect(events[4], isA()); }); + testWidgets('emits point of interest tap when click has placeId', ( + WidgetTester tester, + ) async { + controller = createController() + ..debugSetOverrides( + createMap: (_, _) => map, + circles: circles, + heatmaps: heatmaps, + markers: markers, + polygons: polygons, + polylines: polylines, + groundOverlays: groundOverlays, + ) + ..init(); + + final Stream> capturedEvents = stream.stream.take(1); + + gmaps.event.trigger( + map, + 'click', + gmaps.IconMouseEvent(placeId: 'place-123')..latLng = gmaps.LatLng(0, 0), + ); + + final List> events = await capturedEvents.toList(); + + expect(events, hasLength(1)); + expect(events[0], isA()); + expect((events[0] as PointOfInterestTapEvent).value, const PointOfInterestId('place-123')); + }); + testWidgets('stops listening to map events once disposed', (WidgetTester tester) async { controller = createController() ..debugSetOverrides( diff --git a/packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_plugin_test.dart b/packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_plugin_test.dart index ca1cde5334d9..da40728234f6 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_plugin_test.dart +++ b/packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_plugin_test.dart @@ -467,6 +467,13 @@ void main() { await testStreamFiltering(stream, event); }); + testWidgets('onPointOfInterestTap', (WidgetTester tester) async { + final event = PointOfInterestTapEvent(mapId, const PointOfInterestId('place-123')); + + final Stream stream = plugin.onPointOfInterestTap(mapId: mapId); + + await testStreamFiltering(stream, event); + }); // Map taps testWidgets('onTap', (WidgetTester tester) async { final event = MapTapEvent(mapId, const LatLng(43.3597, -5.8458)); diff --git a/packages/google_maps_flutter/google_maps_flutter_web/example/latest/pubspec.yaml b/packages/google_maps_flutter/google_maps_flutter_web/example/latest/pubspec.yaml index 3c6b3674fd44..bd72568ae37b 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/example/latest/pubspec.yaml +++ b/packages/google_maps_flutter/google_maps_flutter_web/example/latest/pubspec.yaml @@ -8,7 +8,7 @@ environment: dependencies: flutter: sdk: flutter - google_maps_flutter_platform_interface: ^2.16.0 + google_maps_flutter_platform_interface: ^2.17.0 google_maps_flutter_web: path: ../.. web: ^1.0.0 diff --git a/packages/google_maps_flutter/google_maps_flutter_web/lib/google_maps_flutter_web.dart b/packages/google_maps_flutter/google_maps_flutter_web/lib/google_maps_flutter_web.dart index 0413faaf104d..266bebf47050 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/lib/google_maps_flutter_web.dart +++ b/packages/google_maps_flutter/google_maps_flutter_web/lib/google_maps_flutter_web.dart @@ -24,6 +24,7 @@ import 'package:web/web.dart' as web; import 'src/dom_window_extension.dart'; import 'src/google_maps_inspector_web.dart'; +import 'src/map_mouse_event_extension.dart'; import 'src/map_styler.dart'; import 'src/marker_clustering.dart'; import 'src/third_party/to_screen_location/to_screen_location.dart'; diff --git a/packages/google_maps_flutter/google_maps_flutter_web/lib/src/google_maps_controller.dart b/packages/google_maps_flutter/google_maps_flutter_web/lib/src/google_maps_controller.dart index 13e9054d9c7c..dd11bb2d7c65 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/lib/src/google_maps_controller.dart +++ b/packages/google_maps_flutter/google_maps_flutter_web/lib/src/google_maps_controller.dart @@ -289,6 +289,11 @@ class GoogleMapController { _onClickSubscription = map.onClick.listen((gmaps.MapMouseEventOrIconMouseEvent event) { assert(event.latLng != null); if (!_streamController.isClosed) { + final String? placeId = event.placeId; + if (placeId != null) { + _streamController.add(PointOfInterestTapEvent(_mapId, PointOfInterestId(placeId))); + return; + } _streamController.add(MapTapEvent(_mapId, gmLatLngToLatLng(event.latLng!))); } }); diff --git a/packages/google_maps_flutter/google_maps_flutter_web/lib/src/google_maps_flutter_web.dart b/packages/google_maps_flutter/google_maps_flutter_web/lib/src/google_maps_flutter_web.dart index 9e1ce02285d9..9c21fb1af051 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/lib/src/google_maps_flutter_web.dart +++ b/packages/google_maps_flutter/google_maps_flutter_web/lib/src/google_maps_flutter_web.dart @@ -244,6 +244,11 @@ class GoogleMapsPlugin extends GoogleMapsFlutterPlatform { return _events(mapId).whereType(); } + @override + Stream onPointOfInterestTap({required int mapId}) { + return _events(mapId).whereType(); + } + @override Stream onTap({required int mapId}) { return _events(mapId).whereType(); diff --git a/packages/google_maps_flutter/google_maps_flutter_web/lib/src/map_mouse_event_extension.dart b/packages/google_maps_flutter/google_maps_flutter_web/lib/src/map_mouse_event_extension.dart new file mode 100644 index 000000000000..870972a9c83e --- /dev/null +++ b/packages/google_maps_flutter/google_maps_flutter_web/lib/src/map_mouse_event_extension.dart @@ -0,0 +1,22 @@ +// Copyright 2013 The Flutter Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +@JS() +library; + +import 'dart:js_interop'; + +import 'package:google_maps/google_maps.dart' as gmaps; + +/// Exposes the `placeId` property on map click events. +/// +/// [gmaps.MapMouseEventOrIconMouseEvent] only binds `latLng` upstream. POI +/// clicks are [gmaps.IconMouseEvent]s and carry a `placeId`; this extension +/// reads that property without `dart:js_interop_unsafe`. Prefer adding +/// `placeId` to the upstream binding when possible. +extension PlaceIdExtension on gmaps.MapMouseEventOrIconMouseEvent { + /// The place ID of a tapped point of interest, if this event is an icon + /// mouse event. Otherwise `null`. + external String? placeId; +} diff --git a/packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml b/packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml index 4da4852af280..46c529966de9 100644 --- a/packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml +++ b/packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml @@ -2,7 +2,7 @@ name: google_maps_flutter_web description: Web platform implementation of google_maps_flutter repository: https://github.com/flutter/packages/tree/main/packages/google_maps_flutter/google_maps_flutter_web issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22 -version: 0.6.3+1 +version: 0.6.4 environment: sdk: ^3.10.0 @@ -23,7 +23,7 @@ dependencies: flutter_web_plugins: sdk: flutter google_maps: ^8.1.0 - google_maps_flutter_platform_interface: ^2.16.0 + google_maps_flutter_platform_interface: ^2.17.0 sanitize_html: ^2.0.0 stream_transform: ^2.0.0 web: ^1.0.0