Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[google_maps_flutter] Add marker clustering support - iOS implementation #6186

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.7.0

* Adds support for marker clustering.

## 2.6.0

* Updates the minimum allowed verison of the Google Maps SDK to 8.4, for privacy
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 @@ -11,15 +11,16 @@
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
4510D964F3B1259FEDD3ABA6 /* libPods-Runner.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7755F8F4BABC3D6A0BD4048B /* libPods-Runner.a */; };
52CA22A52BDA7BF800233583 /* FLTClusterManagersControllerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 52CA22A42BDA7BF800233583 /* FLTClusterManagersControllerTests.m */; };
6851F3562835BC180032B7C8 /* FLTGoogleMapJSONConversionsConversionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6851F3552835BC180032B7C8 /* FLTGoogleMapJSONConversionsConversionTests.m */; };
68E4726A2836FF0C00BDDDAC /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 68E472692836FF0C00BDDDAC /* MapKit.framework */; };
978B8F6F1D3862AE00F588F7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */; };
97C146F31CF9000F007C117D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 97C146F21CF9000F007C117D /* main.m */; };
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
F269303B2BB389BF00BF17C4 /* assets in Resources */ = {isa = PBXBuildFile; fileRef = F269303A2BB389BF00BF17C4 /* assets */; };
982F2A6C27BADE17003C81F4 /* PartiallyMockedMapView.m in Sources */ = {isa = PBXBuildFile; fileRef = 982F2A6B27BADE17003C81F4 /* PartiallyMockedMapView.m */; };
F269303B2BB389BF00BF17C4 /* assets in Resources */ = {isa = PBXBuildFile; fileRef = F269303A2BB389BF00BF17C4 /* assets */; };
F7151F13265D7ED70028CB91 /* GoogleMapsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F7151F12265D7ED70028CB91 /* GoogleMapsTests.m */; };
F7151F21265D7EE50028CB91 /* GoogleMapsUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = F7151F20265D7EE50028CB91 /* GoogleMapsUITests.m */; };
FC8F35FC8CD533B128950487 /* libPods-RunnerTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F267F68029D1A4E2E4C572A7 /* libPods-RunnerTests.a */; };
Expand Down Expand Up @@ -60,6 +61,7 @@
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
52CA22A42BDA7BF800233583 /* FLTClusterManagersControllerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLTClusterManagersControllerTests.m; sourceTree = "<group>"; };
6851F3552835BC180032B7C8 /* FLTGoogleMapJSONConversionsConversionTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLTGoogleMapJSONConversionsConversionTests.m; sourceTree = "<group>"; };
68E472692836FF0C00BDDDAC /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.0.sdk/System/iOSSupport/System/Library/Frameworks/MapKit.framework; sourceTree = DEVELOPER_DIR; };
733AFAB37683A9DA7512F09C /* Pods-RunnerTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-RunnerTests/Pods-RunnerTests.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -200,6 +202,7 @@
F7151F11265D7ED70028CB91 /* RunnerTests */ = {
isa = PBXGroup;
children = (
52CA22A42BDA7BF800233583 /* FLTClusterManagersControllerTests.m */,
F269303A2BB389BF00BF17C4 /* assets */,
6851F3552835BC180032B7C8 /* FLTGoogleMapJSONConversionsConversionTests.m */,
F7151F12265D7ED70028CB91 /* GoogleMapsTests.m */,
Expand Down Expand Up @@ -466,6 +469,7 @@
files = (
F7151F13265D7ED70028CB91 /* GoogleMapsTests.m in Sources */,
6851F3562835BC180032B7C8 /* FLTGoogleMapJSONConversionsConversionTests.m in Sources */,
52CA22A52BDA7BF800233583 /* FLTClusterManagersControllerTests.m in Sources */,
982F2A6C27BADE17003C81F4 /* PartiallyMockedMapView.m in Sources */,
0DD7B6C32B744EEF00E857FD /* FLTTileProviderControllerTests.m in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

@import google_maps_flutter_ios;
@import google_maps_flutter_ios.Test;
@import XCTest;
@import GoogleMaps;

#import <Flutter/Flutter.h>
#import <OCMock/OCMock.h>
#import "PartiallyMockedMapView.h"

@interface FLTClusterManagersControllerTests : XCTestCase
@end

@implementation FLTClusterManagersControllerTests

- (void)testClustering {
NSObject<FlutterPluginRegistrar> *registrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
CGRect frame = CGRectMake(0, 0, 100, 100);

GMSMapViewOptions *mapViewOptions = [[GMSMapViewOptions alloc] init];
mapViewOptions.frame = frame;
mapViewOptions.camera = [[GMSCameraPosition alloc] initWithLatitude:0 longitude:0 zoom:0];

PartiallyMockedMapView *mapView = [[PartiallyMockedMapView alloc] initWithOptions:mapViewOptions];

NSString *channelName = [NSString stringWithFormat:@"plugins.flutter.dev/google_maps_ios_%d", 0];
FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:channelName
binaryMessenger:registrar.messenger];

NSString *clusterManagerId = @"cm1";

FLTClusterManagersController *clusterManagersController =
[[FLTClusterManagersController alloc] initWithMethodChannel:channel mapView:mapView];
FLTMarkersController *markersController =
[[FLTMarkersController alloc] initWithClusterManagersController:clusterManagersController
channel:channel
mapView:mapView
registrar:registrar];

// Add cluster manager
NSDictionary *clusterManagerToAdd = @{@"clusterManagerId" : clusterManagerId};
[clusterManagersController addClusterManagers:@[ clusterManagerToAdd ]];

// Verify that cluster manager is available
GMUClusterManager *clusterManager =
[clusterManagersController clusterManagerWithIdentifier:clusterManagerId];
XCTAssertNotNil(clusterManager, @"Cluster Manager should not be nil");

// Add markers
NSString *markerId1 = @"m1";
NSString *markerId2 = @"m2";

NSDictionary *marker1 =
@{@"markerId" : markerId1, @"position" : @[ @0, @0 ], @"clusterManagerId" : clusterManagerId};
NSDictionary *marker2 =
@{@"markerId" : markerId2, @"position" : @[ @0, @0 ], @"clusterManagerId" : clusterManagerId};
[markersController addMarkers:@[ marker1, marker2 ]];

// Invoke clustering
[clusterManagersController invokeClusteringForEachClusterManager];

// Verify that the markers were added to the cluster manager
FlutterResult resultObject1 = ^(id _Nullable result) {
NSArray *clusters = (NSArray *)result;
for (NSDictionary *cluster in clusters) {
NSString *cmId = cluster[@"clusterManagerId"];
XCTAssertNotNil(cmId, @"Cluster Manager Identifier should not be nil");
if ([cmId isEqualToString:clusterManagerId]) {
NSArray *markerIds = cluster[@"markerIds"];
XCTAssertEqual(markerIds.count, 2, @"Cluster should contain two marker");
XCTAssertTrue([markerIds containsObject:markerId1], @"Cluster should contain markerId1");
XCTAssertTrue([markerIds containsObject:markerId2], @"Cluster should contain markerId2");
return;
}
}
// Cluster for clustermanager not found, fail the test
XCTFail(@"Cluster manager not found");
};
[clusterManagersController serializeClustersWithIdentifier:clusterManagerId result:resultObject1];

[markersController removeMarkersWithIdentifiers:@[ markerId2 ]];

// Verify that the marker2 is removed from the clusterManager
FlutterResult resultObject2 = ^(id _Nullable result) {
NSArray *clusters = (NSArray *)result;
for (NSDictionary *cluster in clusters) {
NSString *cmId = cluster[@"clusterManagerId"];
XCTAssertNotNil(cmId, @"Cluster Manager ID should not be nil");
if ([cmId isEqualToString:clusterManagerId]) {
NSArray *markerIds = cluster[@"markerIds"];
XCTAssertEqual(markerIds.count, 1, @"Cluster should contain one marker");
XCTAssertTrue([markerIds containsObject:markerId1], @"Cluster should contain markerId1");
return;
}
}
// Cluster for clustermanager not found, fail the test
XCTFail(@"Cluster manager not found");
};
[clusterManagersController serializeClustersWithIdentifier:clusterManagerId result:resultObject2];

[markersController removeMarkersWithIdentifiers:@[ markerId1 ]];

// Verify that all markers are removed from clusterManager
FlutterResult resultObject3 = ^(id _Nullable result) {
NSArray *clusters = (NSArray *)result;
XCTAssertEqual(clusters.count, 0, @"Cluster Manager should not contain any clusters");
};
[clusterManagersController serializeClustersWithIdentifier:clusterManagerId result:resultObject3];

// Remove cluster manager
[clusterManagersController removeClusterManagersWithIdentifiers:@[ clusterManagerId ]];

// Verify that the cluster manager is removed
clusterManager = [clusterManagersController clusterManagerWithIdentifier:clusterManagerId];
XCTAssertNil(clusterManager, @"Cluster Manager should be nil");
}

@end
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