Skip to content

Commit 26fd75a

Browse files
committed
[google_maps_flutter_ios] Add advanced marker support
1 parent fb5f02f commit 26fd75a

32 files changed

+2333
-333
lines changed

packages/google_maps_flutter/google_maps_flutter_ios/example/ios14/integration_test/google_maps_test.dart

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const CameraPosition _kInitialCameraPosition = CameraPosition(
2121
target: _kInitialMapCenter,
2222
zoom: _kInitialZoomLevel,
2323
);
24-
const String _kCloudMapId = '000000000000000'; // Dummy map ID.
24+
const String _kMapId = '000000000000000'; // Dummy map ID.
2525

2626
// The tolerance value for floating-point comparisons in the tests.
2727
// This value was selected as the minimum possible value that the test passes.
@@ -1388,6 +1388,104 @@ void main() {
13881388
}
13891389
});
13901390

1391+
testWidgets('advanced markers clustering', (WidgetTester tester) async {
1392+
final Key key = GlobalKey();
1393+
const int clusterManagersAmount = 2;
1394+
const int markersPerClusterManager = 5;
1395+
final Map<MarkerId, AdvancedMarker> markers = <MarkerId, AdvancedMarker>{};
1396+
final Set<ClusterManager> clusterManagers = <ClusterManager>{};
1397+
1398+
for (int i = 0; i < clusterManagersAmount; i++) {
1399+
final ClusterManagerId clusterManagerId = ClusterManagerId(
1400+
'cluster_manager_$i',
1401+
);
1402+
final ClusterManager clusterManager = ClusterManager(
1403+
clusterManagerId: clusterManagerId,
1404+
);
1405+
clusterManagers.add(clusterManager);
1406+
}
1407+
1408+
for (final ClusterManager cm in clusterManagers) {
1409+
for (int i = 0; i < markersPerClusterManager; i++) {
1410+
final MarkerId markerId = MarkerId(
1411+
'${cm.clusterManagerId.value}_marker_$i',
1412+
);
1413+
final AdvancedMarker marker = AdvancedMarker(
1414+
markerId: markerId,
1415+
clusterManagerId: cm.clusterManagerId,
1416+
position: LatLng(
1417+
_kInitialMapCenter.latitude + i,
1418+
_kInitialMapCenter.longitude,
1419+
),
1420+
);
1421+
markers[markerId] = marker;
1422+
}
1423+
}
1424+
1425+
final Completer<ExampleGoogleMapController> controllerCompleter =
1426+
Completer<ExampleGoogleMapController>();
1427+
1428+
final GoogleMapsInspectorPlatform inspector =
1429+
GoogleMapsInspectorPlatform.instance!;
1430+
1431+
await tester.pumpWidget(
1432+
Directionality(
1433+
textDirection: TextDirection.ltr,
1434+
child: ExampleGoogleMap(
1435+
key: key,
1436+
initialCameraPosition: _kInitialCameraPosition,
1437+
clusterManagers: clusterManagers,
1438+
markerType: MarkerType.advancedMarker,
1439+
markers: Set<Marker>.of(markers.values),
1440+
onMapCreated: (ExampleGoogleMapController googleMapController) {
1441+
controllerCompleter.complete(googleMapController);
1442+
},
1443+
),
1444+
),
1445+
);
1446+
1447+
final ExampleGoogleMapController controller =
1448+
await controllerCompleter.future;
1449+
1450+
for (final ClusterManager cm in clusterManagers) {
1451+
final List<Cluster> clusters = await inspector.getClusters(
1452+
mapId: controller.mapId,
1453+
clusterManagerId: cm.clusterManagerId,
1454+
);
1455+
final int markersAmountForClusterManager = clusters
1456+
.map<int>((Cluster cluster) => cluster.count)
1457+
.reduce((int value, int element) => value + element);
1458+
expect(markersAmountForClusterManager, markersPerClusterManager);
1459+
}
1460+
1461+
// Remove markers from clusterManagers and test that clusterManagers are empty.
1462+
for (final MapEntry<MarkerId, AdvancedMarker> entry in markers.entries) {
1463+
markers[entry.key] = _copyAdvancedMarkerWithClusterManagerId(
1464+
entry.value,
1465+
null,
1466+
);
1467+
}
1468+
await tester.pumpWidget(
1469+
Directionality(
1470+
textDirection: TextDirection.ltr,
1471+
child: ExampleGoogleMap(
1472+
key: key,
1473+
initialCameraPosition: _kInitialCameraPosition,
1474+
clusterManagers: clusterManagers,
1475+
markers: Set<Marker>.of(markers.values),
1476+
),
1477+
),
1478+
);
1479+
1480+
for (final ClusterManager cm in clusterManagers) {
1481+
final List<Cluster> clusters = await inspector.getClusters(
1482+
mapId: controller.mapId,
1483+
clusterManagerId: cm.clusterManagerId,
1484+
);
1485+
expect(clusters.length, 0);
1486+
}
1487+
});
1488+
13911489
testWidgets('testSetStyleMapId', (WidgetTester tester) async {
13921490
final Key key = GlobalKey();
13931491

@@ -1397,7 +1495,7 @@ void main() {
13971495
child: ExampleGoogleMap(
13981496
key: key,
13991497
initialCameraPosition: _kInitialCameraPosition,
1400-
cloudMapId: _kCloudMapId,
1498+
mapId: _kMapId,
14011499
),
14021500
),
14031501
);
@@ -2099,6 +2197,39 @@ void main() {
20992197
// Hanging in CI, https://github.com/flutter/flutter/issues/166139
21002198
skip: true,
21012199
);
2200+
2201+
testWidgets('markerWithPinConfig', (WidgetTester tester) async {
2202+
final Set<AdvancedMarker> markers = <AdvancedMarker>{
2203+
AdvancedMarker(
2204+
markerId: const MarkerId('1'),
2205+
icon: BitmapDescriptor.pinConfig(
2206+
backgroundColor: Colors.green,
2207+
borderColor: Colors.greenAccent,
2208+
glyph: const TextGlyph(text: 'A', textColor: Colors.white),
2209+
),
2210+
),
2211+
};
2212+
2213+
final Completer<ExampleGoogleMapController> controllerCompleter =
2214+
Completer<ExampleGoogleMapController>();
2215+
2216+
await tester.pumpWidget(
2217+
Directionality(
2218+
textDirection: ui.TextDirection.ltr,
2219+
child: ExampleGoogleMap(
2220+
initialCameraPosition: const CameraPosition(
2221+
target: LatLng(10.0, 20.0),
2222+
),
2223+
markers: markers,
2224+
markerType: MarkerType.advancedMarker,
2225+
onMapCreated: (ExampleGoogleMapController controller) =>
2226+
controllerCompleter.complete(controller),
2227+
),
2228+
),
2229+
);
2230+
await tester.pumpAndSettle();
2231+
await controllerCompleter.future;
2232+
});
21022233
}
21032234

21042235
class _DebugTileProvider implements TileProvider {
@@ -2285,3 +2416,29 @@ Future<void> _checkCameraUpdateByType(
22852416
expect(currentPosition.zoom, wrapMatcher(equals(_kInitialZoomLevel - 1)));
22862417
}
22872418
}
2419+
2420+
AdvancedMarker _copyAdvancedMarkerWithClusterManagerId(
2421+
AdvancedMarker marker,
2422+
ClusterManagerId? clusterManagerId,
2423+
) {
2424+
return AdvancedMarker(
2425+
markerId: marker.markerId,
2426+
alpha: marker.alpha,
2427+
anchor: marker.anchor,
2428+
consumeTapEvents: marker.consumeTapEvents,
2429+
draggable: marker.draggable,
2430+
flat: marker.flat,
2431+
icon: marker.icon,
2432+
infoWindow: marker.infoWindow,
2433+
position: marker.position,
2434+
rotation: marker.rotation,
2435+
visible: marker.visible,
2436+
zIndex: marker.zIndex.toInt(),
2437+
onTap: marker.onTap,
2438+
onDragStart: marker.onDragStart,
2439+
onDrag: marker.onDrag,
2440+
onDragEnd: marker.onDragEnd,
2441+
clusterManagerId: clusterManagerId,
2442+
collisionBehavior: marker.collisionBehavior,
2443+
);
2444+
}

packages/google_maps_flutter/google_maps_flutter_ios/example/ios14/ios/RunnerTests/FGMClusterManagersControllerTests.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ - (void)testClustering {
3535
[[FLTMarkersController alloc] initWithMapView:mapView
3636
callbackHandler:handler
3737
clusterManagersController:clusterManagersController
38-
registrar:registrar];
38+
registrar:registrar
39+
markerType:FGMPlatformMarkerTypeMarker];
3940

4041
// Add cluster managers.
4142
NSString *clusterManagerId = @"cm";
@@ -71,7 +72,8 @@ - (void)testClustering {
7172
visible:YES
7273
zIndex:1
7374
markerId:markerId1
74-
clusterManagerId:clusterManagerId];
75+
clusterManagerId:clusterManagerId
76+
collisionBehavior:NULL];
7577
FGMPlatformMarker *marker2 = [FGMPlatformMarker makeWithAlpha:1
7678
anchor:zeroPoint
7779
consumeTapEvents:NO
@@ -84,7 +86,8 @@ - (void)testClustering {
8486
visible:YES
8587
zIndex:1
8688
markerId:markerId2
87-
clusterManagerId:clusterManagerId];
89+
clusterManagerId:clusterManagerId
90+
collisionBehavior:NULL];
8891

8992
[markersController addMarkers:@[ marker1, marker2 ]];
9093

0 commit comments

Comments
 (0)