@@ -18,6 +18,14 @@ class GoogleMapController {
1818 /// The mapId for this controller
1919 final int mapId;
2020
21+ /// List of active stream subscriptions for map events.
22+ ///
23+ /// This list keeps track of all event subscriptions created for the map,
24+ /// including camera movements, marker interactions, and other map events.
25+ /// These subscriptions should be disposed when the controller is disposed.
26+ final List <StreamSubscription <dynamic >> _streamSubscriptions =
27+ < StreamSubscription <dynamic >> [];
28+
2129 /// Initialize control of a [GoogleMap] with [id] .
2230 ///
2331 /// Mainly for internal use when instantiating a [GoogleMapController] passed
@@ -38,53 +46,85 @@ class GoogleMapController {
3846
3947 void _connectStreams (int mapId) {
4048 if (_googleMapState.widget.onCameraMoveStarted != null ) {
41- GoogleMapsFlutterPlatform .instance
42- .onCameraMoveStarted (mapId: mapId)
43- .listen ((_) => _googleMapState.widget.onCameraMoveStarted !());
49+ _streamSubscriptions.add (
50+ GoogleMapsFlutterPlatform .instance
51+ .onCameraMoveStarted (mapId: mapId)
52+ .listen ((_) => _googleMapState.widget.onCameraMoveStarted !()),
53+ );
4454 }
4555 if (_googleMapState.widget.onCameraMove != null ) {
46- GoogleMapsFlutterPlatform .instance.onCameraMove (mapId: mapId).listen (
47- (CameraMoveEvent e) => _googleMapState.widget.onCameraMove !(e.value));
56+ _streamSubscriptions.add (
57+ GoogleMapsFlutterPlatform .instance.onCameraMove (mapId: mapId).listen (
58+ (CameraMoveEvent e) =>
59+ _googleMapState.widget.onCameraMove !(e.value),
60+ ),
61+ );
4862 }
4963 if (_googleMapState.widget.onCameraIdle != null ) {
50- GoogleMapsFlutterPlatform .instance
51- .onCameraIdle (mapId: mapId)
52- .listen ((_) => _googleMapState.widget.onCameraIdle !());
64+ _streamSubscriptions.add (
65+ GoogleMapsFlutterPlatform .instance
66+ .onCameraIdle (mapId: mapId)
67+ .listen ((_) => _googleMapState.widget.onCameraIdle !()),
68+ );
5369 }
54- GoogleMapsFlutterPlatform .instance
55- .onMarkerTap (mapId: mapId)
56- .listen ((MarkerTapEvent e) => _googleMapState.onMarkerTap (e.value));
57- GoogleMapsFlutterPlatform .instance.onMarkerDragStart (mapId: mapId).listen (
58- (MarkerDragStartEvent e) =>
59- _googleMapState.onMarkerDragStart (e.value, e.position));
60- GoogleMapsFlutterPlatform .instance.onMarkerDrag (mapId: mapId).listen (
61- (MarkerDragEvent e) =>
62- _googleMapState.onMarkerDrag (e.value, e.position));
63- GoogleMapsFlutterPlatform .instance.onMarkerDragEnd (mapId: mapId).listen (
64- (MarkerDragEndEvent e) =>
65- _googleMapState.onMarkerDragEnd (e.value, e.position));
66- GoogleMapsFlutterPlatform .instance.onInfoWindowTap (mapId: mapId).listen (
67- (InfoWindowTapEvent e) => _googleMapState.onInfoWindowTap (e.value));
68- GoogleMapsFlutterPlatform .instance
69- .onPolylineTap (mapId: mapId)
70- .listen ((PolylineTapEvent e) => _googleMapState.onPolylineTap (e.value));
71- GoogleMapsFlutterPlatform .instance
72- .onPolygonTap (mapId: mapId)
73- .listen ((PolygonTapEvent e) => _googleMapState.onPolygonTap (e.value));
74- GoogleMapsFlutterPlatform .instance
75- .onCircleTap (mapId: mapId)
76- .listen ((CircleTapEvent e) => _googleMapState.onCircleTap (e.value));
77- GoogleMapsFlutterPlatform .instance.onGroundOverlayTap (mapId: mapId).listen (
78- (GroundOverlayTapEvent e) =>
79- _googleMapState.onGroundOverlayTap (e.value));
80- GoogleMapsFlutterPlatform .instance
81- .onTap (mapId: mapId)
82- .listen ((MapTapEvent e) => _googleMapState.onTap (e.position));
83- GoogleMapsFlutterPlatform .instance.onLongPress (mapId: mapId).listen (
84- (MapLongPressEvent e) => _googleMapState.onLongPress (e.position));
85- GoogleMapsFlutterPlatform .instance
86- .onClusterTap (mapId: mapId)
87- .listen ((ClusterTapEvent e) => _googleMapState.onClusterTap (e.value));
70+ _streamSubscriptions.add (
71+ GoogleMapsFlutterPlatform .instance
72+ .onMarkerTap (mapId: mapId)
73+ .listen ((MarkerTapEvent e) => _googleMapState.onMarkerTap (e.value)),
74+ );
75+ _streamSubscriptions.add (
76+ GoogleMapsFlutterPlatform .instance.onMarkerDragStart (mapId: mapId).listen (
77+ (MarkerDragStartEvent e) =>
78+ _googleMapState.onMarkerDragStart (e.value, e.position),
79+ ),
80+ );
81+ _streamSubscriptions.add (
82+ GoogleMapsFlutterPlatform .instance.onMarkerDrag (mapId: mapId).listen (
83+ (MarkerDragEvent e) =>
84+ _googleMapState.onMarkerDrag (e.value, e.position),
85+ ),
86+ );
87+ _streamSubscriptions.add (
88+ GoogleMapsFlutterPlatform .instance.onMarkerDragEnd (mapId: mapId).listen (
89+ (MarkerDragEndEvent e) =>
90+ _googleMapState.onMarkerDragEnd (e.value, e.position),
91+ ),
92+ );
93+ _streamSubscriptions.add (
94+ GoogleMapsFlutterPlatform .instance.onInfoWindowTap (mapId: mapId).listen (
95+ (InfoWindowTapEvent e) => _googleMapState.onInfoWindowTap (e.value),
96+ ),
97+ );
98+ _streamSubscriptions.add (
99+ GoogleMapsFlutterPlatform .instance.onPolylineTap (mapId: mapId).listen (
100+ (PolylineTapEvent e) => _googleMapState.onPolylineTap (e.value),
101+ ),
102+ );
103+ _streamSubscriptions.add (
104+ GoogleMapsFlutterPlatform .instance.onPolygonTap (mapId: mapId).listen (
105+ (PolygonTapEvent e) => _googleMapState.onPolygonTap (e.value),
106+ ),
107+ );
108+ _streamSubscriptions.add (
109+ GoogleMapsFlutterPlatform .instance
110+ .onCircleTap (mapId: mapId)
111+ .listen ((CircleTapEvent e) => _googleMapState.onCircleTap (e.value)),
112+ );
113+ _streamSubscriptions.add (
114+ GoogleMapsFlutterPlatform .instance
115+ .onTap (mapId: mapId)
116+ .listen ((MapTapEvent e) => _googleMapState.onTap (e.position)),
117+ );
118+ _streamSubscriptions.add (
119+ GoogleMapsFlutterPlatform .instance.onLongPress (mapId: mapId).listen (
120+ (MapLongPressEvent e) => _googleMapState.onLongPress (e.position),
121+ ),
122+ );
123+ _streamSubscriptions.add (
124+ GoogleMapsFlutterPlatform .instance.onClusterTap (mapId: mapId).listen (
125+ (ClusterTapEvent e) => _googleMapState.onClusterTap (e.value),
126+ ),
127+ );
88128 }
89129
90130 /// Updates configuration options of the map user interface.
@@ -321,6 +361,11 @@ class GoogleMapController {
321361
322362 /// Disposes of the platform resources
323363 void dispose () {
364+ for (final StreamSubscription <dynamic > streamSubscription
365+ in _streamSubscriptions) {
366+ streamSubscription.cancel ();
367+ }
368+ _streamSubscriptions.clear ();
324369 GoogleMapsFlutterPlatform .instance.dispose (mapId: mapId);
325370 }
326371}
0 commit comments