Skip to content

Commit

Permalink
[google_maps_flutter_ios] Add support for cluster markering
Browse files Browse the repository at this point in the history
  • Loading branch information
jokerttu committed Mar 1, 2024
1 parent 268f7d5 commit cc42eb1
Show file tree
Hide file tree
Showing 31 changed files with 1,112 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ Anton Borries <mail@antonborri.es>
Alex Li <google@alexv525.com>
Rahul Raj <64.rahulraj@gmail.com>
Taha Tesser <tesser@gmail.com>
Joonas Kerttula <joonas.kerttula@codemate.com>
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.6.0

* Adds support for marker clustering.

## 2.5.0

* Adds support for `MapConfiguration.style`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,85 @@ void main() {
},
);

testWidgets('marker clustering', (WidgetTester tester) async {
final Key key = GlobalKey();
const int clusterManagersAmount = 2;
const int markersPerClusterManager = 5;
final Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
final Set<ClusterManager> clusterManagers = <ClusterManager>{};

for (int i = 0; i < clusterManagersAmount; i++) {
final ClusterManagerId clusterManagerId =
ClusterManagerId('cluster_manager_$i');
final ClusterManager clusterManager =
ClusterManager(clusterManagerId: clusterManagerId);
clusterManagers.add(clusterManager);
}

for (final ClusterManager cm in clusterManagers) {
for (int i = 0; i < markersPerClusterManager; i++) {
final MarkerId markerId =
MarkerId('${cm.clusterManagerId.value}_marker_$i');
final Marker marker = Marker(
markerId: markerId,
clusterManagerId: cm.clusterManagerId,
position: LatLng(
_kInitialMapCenter.latitude + i, _kInitialMapCenter.longitude));
markers[markerId] = marker;
}
}

final Completer<ExampleGoogleMapController> controllerCompleter =
Completer<ExampleGoogleMapController>();

final GoogleMapsInspectorPlatform inspector =
GoogleMapsInspectorPlatform.instance!;

await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: ExampleGoogleMap(
key: key,
initialCameraPosition: _kInitialCameraPosition,
clusterManagers: clusterManagers,
markers: Set<Marker>.of(markers.values),
onMapCreated: (ExampleGoogleMapController googleMapController) {
controllerCompleter.complete(googleMapController);
},
),
));

final ExampleGoogleMapController controller =
await controllerCompleter.future;

for (final ClusterManager cm in clusterManagers) {
final List<Cluster> clusters = await inspector.getClusters(
mapId: controller.mapId, clusterManagerId: cm.clusterManagerId);
final int markersAmountForClusterManager = clusters
.map<int>((Cluster cluster) => cluster.count)
.reduce((int value, int element) => value + element);
expect(markersAmountForClusterManager, markersPerClusterManager);
}

// Remove markers from clusterManagers and test that clusterManagers are empty.
for (final MapEntry<MarkerId, Marker> entry in markers.entries) {
markers[entry.key] = _copyMarkerWithClusterManagerId(entry.value, null);
}
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: ExampleGoogleMap(
key: key,
initialCameraPosition: _kInitialCameraPosition,
clusterManagers: clusterManagers,
markers: Set<Marker>.of(markers.values)),
));

for (final ClusterManager cm in clusterManagers) {
final List<Cluster> clusters = await inspector.getClusters(
mapId: controller.mapId, clusterManagerId: cm.clusterManagerId);
expect(clusters.length, 0);
}
});

testWidgets('testSetStyleMapId', (WidgetTester tester) async {
final Key key = GlobalKey();

Expand Down Expand Up @@ -1145,3 +1224,26 @@ class _DebugTileProvider implements TileProvider {
return Tile(width, height, byteData);
}
}

Marker _copyMarkerWithClusterManagerId(
Marker marker, ClusterManagerId? clusterManagerId) {
return Marker(
markerId: marker.markerId,
alpha: marker.alpha,
anchor: marker.anchor,
consumeTapEvents: marker.consumeTapEvents,
draggable: marker.draggable,
flat: marker.flat,
icon: marker.icon,
infoWindow: marker.infoWindow,
position: marker.position,
rotation: marker.rotation,
visible: marker.visible,
zIndex: marker.zIndex,
onTap: marker.onTap,
onDragStart: marker.onDragStart,
onDrag: marker.onDrag,
onDragEnd: marker.onDragEnd,
clusterManagerId: clusterManagerId,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:flutter/material.dart';
import 'package:maps_example_dart/animate_camera.dart';
import 'package:maps_example_dart/clustering.dart';
import 'package:maps_example_dart/lite_mode.dart';
import 'package:maps_example_dart/map_click.dart';
import 'package:maps_example_dart/map_coordinates.dart';
Expand Down Expand Up @@ -40,6 +41,7 @@ void main() {
SnapshotPage(),
LiteModePage(),
TileOverlayPage(),
ClusteringPage(),
MapIdPage(),
])));
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../../
google_maps_flutter_platform_interface: ^2.5.0
google_maps_flutter_platform_interface: ^2.6.0
maps_example_dart:
path: ../shared/maps_example_dart/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:flutter/material.dart';
import 'package:maps_example_dart/animate_camera.dart';
import 'package:maps_example_dart/clustering.dart';
import 'package:maps_example_dart/lite_mode.dart';
import 'package:maps_example_dart/map_click.dart';
import 'package:maps_example_dart/map_coordinates.dart';
Expand Down Expand Up @@ -39,6 +40,7 @@ void main() {
PaddingPage(),
SnapshotPage(),
LiteModePage(),
ClusteringPage(),
TileOverlayPage(),
MapIdPage(),
])));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../../
google_maps_flutter_platform_interface: ^2.5.0
google_maps_flutter_platform_interface: ^2.6.0
maps_example_dart:
path: ../shared/maps_example_dart/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:flutter/material.dart';
import 'package:maps_example_dart/animate_camera.dart';
import 'package:maps_example_dart/clustering.dart';
import 'package:maps_example_dart/lite_mode.dart';
import 'package:maps_example_dart/map_click.dart';
import 'package:maps_example_dart/map_coordinates.dart';
Expand Down Expand Up @@ -40,6 +41,7 @@ void main() {
SnapshotPage(),
LiteModePage(),
TileOverlayPage(),
ClusteringPage(),
MapIdPage(),
])));
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../../
google_maps_flutter_platform_interface: ^2.5.0
google_maps_flutter_platform_interface: ^2.6.0
maps_example_dart:
path: ../shared/maps_example_dart/

Expand Down

0 comments on commit cc42eb1

Please sign in to comment.