Skip to content

Commit 0e22076

Browse files
authored
[google_maps_flutter_web] Omit styles when cloudMapId is set (#9869)
When `cloudMapId` is set, styles must be omitted to avoid conflicts. Now we set `MapOptions.mapId` and leave `styles` null in that case. Tests: add `cloud_map_styles_test` (web integration) to verify: - cloudMapId present ⇒ styles omitted, mapId set - no cloudMapId ⇒ styles applied Fixes flutter/flutter#161951 **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent ed67a29 commit 0e22076

File tree

4 files changed

+140
-3
lines changed

4 files changed

+140
-3
lines changed

packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.14+2
2+
3+
* Fixes a bug where using `cloudMapId` for cloud-based styling would fail if the `style` property was also present.
4+
15
## 0.5.14+1
26

37
* Stops processing events and cancels subscriptions when controller is disposed.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
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 'dart:async';
6+
import 'package:flutter/widgets.dart'
7+
show Directionality, SizedBox, TextDirection;
8+
import 'package:flutter_test/flutter_test.dart';
9+
import 'package:google_maps/google_maps.dart' as gmaps;
10+
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'
11+
show
12+
CameraPosition,
13+
LatLng,
14+
MapConfiguration,
15+
MapEvent,
16+
MapWidgetConfiguration;
17+
import 'package:google_maps_flutter_web/google_maps_flutter_web.dart'
18+
show GoogleMapController;
19+
import 'package:integration_test/integration_test.dart';
20+
21+
void main() {
22+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
23+
24+
MapWidgetConfiguration cfg() => const MapWidgetConfiguration(
25+
initialCameraPosition: CameraPosition(target: LatLng(0, 0), zoom: 1),
26+
textDirection: TextDirection.ltr,
27+
);
28+
29+
testWidgets('cloudMapId present => mapId set & styles omitted', (
30+
WidgetTester tester,
31+
) async {
32+
const MapConfiguration testMapConfig = MapConfiguration(
33+
mapId: 'test-cloud-map-id',
34+
);
35+
36+
await tester.pumpWidget(
37+
const Directionality(textDirection: TextDirection.ltr, child: SizedBox()),
38+
);
39+
40+
final StreamController<MapEvent<Object?>> stream =
41+
StreamController<MapEvent<Object?>>();
42+
addTearDown(() {
43+
// Stream is closed by controller.dispose()
44+
});
45+
46+
gmaps.MapOptions? captured;
47+
48+
final GoogleMapController controller = GoogleMapController(
49+
mapId: 1, // Internal controller ID
50+
streamController: stream,
51+
widgetConfiguration: cfg(),
52+
mapConfiguration: testMapConfig, // cloudMapId is set here
53+
);
54+
55+
controller.debugSetOverrides(
56+
setOptions: (gmaps.MapOptions options) {
57+
captured = options;
58+
},
59+
);
60+
61+
final List<gmaps.MapTypeStyle> styles = <gmaps.MapTypeStyle>[
62+
gmaps.MapTypeStyle()
63+
..featureType = 'road'
64+
..elementType = 'geometry',
65+
];
66+
67+
controller.updateStyles(styles);
68+
69+
await tester.pump();
70+
71+
expect(captured, isNotNull);
72+
expect(captured!.mapId, testMapConfig.mapId);
73+
expect(
74+
captured!.styles == null || captured!.styles!.isEmpty,
75+
isTrue,
76+
reason: 'When cloudMapId is set, styles must not be applied.',
77+
);
78+
79+
controller.dispose();
80+
});
81+
82+
testWidgets('no cloudMapId => styles applied', (WidgetTester tester) async {
83+
await tester.pumpWidget(
84+
const Directionality(textDirection: TextDirection.ltr, child: SizedBox()),
85+
);
86+
87+
final StreamController<MapEvent<Object?>> stream =
88+
StreamController<MapEvent<Object?>>();
89+
addTearDown(() {
90+
// Stream is closed by controller.dispose()
91+
});
92+
93+
gmaps.MapOptions? captured;
94+
final GoogleMapController controller = GoogleMapController(
95+
mapId: 2, // Internal controller ID
96+
streamController: stream,
97+
widgetConfiguration: cfg(),
98+
);
99+
100+
controller.debugSetOverrides(
101+
setOptions: (gmaps.MapOptions options) {
102+
captured = options;
103+
},
104+
);
105+
106+
final List<gmaps.MapTypeStyle> styles = <gmaps.MapTypeStyle>[
107+
gmaps.MapTypeStyle()
108+
..featureType = 'poi'
109+
..elementType = 'labels',
110+
];
111+
112+
controller.updateStyles(styles);
113+
114+
await tester.pump();
115+
116+
expect(captured, isNotNull);
117+
expect(
118+
captured!.mapId,
119+
anyOf(isNull, isEmpty),
120+
reason: 'mapId should be empty/null when no Cloud Map is used.',
121+
);
122+
expect(captured!.styles, isNotNull);
123+
expect(
124+
captured!.styles,
125+
isNotEmpty,
126+
reason: 'When cloudMapId is null, styles should be applied.',
127+
);
128+
129+
controller.dispose();
130+
});
131+
}

packages/google_maps_flutter/google_maps_flutter_web/lib/src/convert.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ gmaps.MapOptions _configurationAndStyleToGmapsOptions(
121121
options.fullscreenControl = false;
122122
options.streetViewControl = false;
123123

124-
// See updateMapConfiguration for why this is not using configuration.style.
125-
options.styles = styles;
124+
// If using cloud map, do not set options.styles
125+
if (configuration.cloudMapId == null) {
126+
options.styles = styles;
127+
}
126128

127129
options.mapId = configuration.cloudMapId;
128130

packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_maps_flutter_web
22
description: Web platform implementation of google_maps_flutter
33
repository: https://github.com/flutter/packages/tree/main/packages/google_maps_flutter/google_maps_flutter_web
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
5-
version: 0.5.14+1
5+
version: 0.5.14+2
66

77
environment:
88
sdk: ^3.7.0

0 commit comments

Comments
 (0)