Skip to content

Commit

Permalink
[google_maps_flutter_ios] Improve clustering support
Browse files Browse the repository at this point in the history
  • Loading branch information
jokerttu committed Mar 7, 2024
1 parent cec208b commit 1669b76
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,25 @@ NS_ASSUME_NONNULL_BEGIN
/// @return GMUClusterManager if found otherwise nil.
- (GMUClusterManager *)clusterManagerWithIdentifier:(NSString *)identifier;

/// Converts all clusters from the specific ClusterManager to result object response
/// and updates that to the provided result object.
/// Converts clusters managed by the specified ClusterManager to
/// serializable array of clusters.
///
/// @param identifier identifier of the ClusterManager.
/// @param result FlutterResult object to be updated with cluster data.
- (void)clustersWithIdentifier:(NSString *)identifier result:(FlutterResult)result;
/// This method fetches and serializes clusters at the current zoom
/// level from the ClusterManager identified by the given identifier.
/// If the specified ClusterManager identifier does not exist, an error
/// is returned through the FlutterResult object.
///
/// @param identifier The identifier of the ClusterManager to serialize.
/// @param result The FlutterResult object to be updated with cluster data.
- (void)serializeClustersWithIdentifier:(NSString *)identifier result:(FlutterResult)result;

/// Called when cluster marker is tapped on the map.
///
/// @param cluster GMUStaticCluster object.
- (void)didTapOnCluster:(GMUStaticCluster *)cluster;

/// Calls cluster method of all ClusterManagers.
- (void)clusterAll;
- (void)invokeClusteringForEachClusterManager;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@interface FLTClusterManagersController ()

@property(strong, nonatomic)
NSMutableDictionary<NSString *, GMUClusterManager *> *clusterManagerIdToManagers;
NSMutableDictionary<NSString *, GMUClusterManager *> *clusterManagerIdentifierToManagers;
@property(strong, nonatomic) FlutterMethodChannel *methodChannel;
@property(strong, nonatomic) GMSMapView *mapView;

Expand All @@ -22,7 +22,7 @@ - (instancetype)initWithMethodChannel:(FlutterMethodChannel *)methodChannel
if (self) {
_methodChannel = methodChannel;
_mapView = mapView;
_clusterManagerIdToManagers = [[NSMutableDictionary alloc] init];
_clusterManagerIdentifierToManagers = [[NSMutableDictionary alloc] init];
}
return self;
}
Expand All @@ -38,33 +38,35 @@ - (void)addClusterManagers:(NSArray<NSDictionary *> *)clusterManagersToAdd {
GMUClusterManager *clusterManager = [[GMUClusterManager alloc] initWithMap:self.mapView
algorithm:algorithm
renderer:renderer];
self.clusterManagerIdToManagers[identifier] = clusterManager;
self.clusterManagerIdentifierToManagers[identifier] = clusterManager;
}
}

- (void)removeClusterManagersWithIdentifiers:(NSArray<NSString *> *)identifiers {
for (NSString *identifier in identifiers) {
GMUClusterManager *clusterManager = [self.clusterManagerIdToManagers objectForKey:identifier];
GMUClusterManager *clusterManager =
[self.clusterManagerIdentifierToManagers objectForKey:identifier];
if (!clusterManager) {
continue;
}
[clusterManager clearItems];
[self.clusterManagerIdToManagers removeObjectForKey:identifier];
[self.clusterManagerIdentifierToManagers removeObjectForKey:identifier];
}
}

- (GMUClusterManager *)clusterManagerWithIdentifier:(NSString *)identifier {
return [self.clusterManagerIdToManagers objectForKey:identifier];
return [self.clusterManagerIdentifierToManagers objectForKey:identifier];
}

- (void)clusterAll {
for (GMUClusterManager *clusterManager in [self.clusterManagerIdToManagers allValues]) {
- (void)invokeClusteringForEachClusterManager {
for (GMUClusterManager *clusterManager in [self.clusterManagerIdentifierToManagers allValues]) {
[clusterManager cluster];
}
}

- (void)clustersWithIdentifier:(NSString *)identifier result:(FlutterResult)result {
GMUClusterManager *clusterManager = [self.clusterManagerIdToManagers objectForKey:identifier];
- (void)serializeClustersWithIdentifier:(NSString *)identifier result:(FlutterResult)result {
GMUClusterManager *clusterManager =
[self.clusterManagerIdentifierToManagers objectForKey:identifier];

if (!clusterManager) {
result([FlutterError errorWithCode:@"Invalid clusterManagerId"
Expand All @@ -81,7 +83,7 @@ - (void)clustersWithIdentifier:(NSString *)identifier result:(FlutterResult)resu
NSArray<id<GMUCluster>> *clusters = [clusterManager.algorithm clustersAtZoom:integralZoom];
for (id<GMUCluster> cluster in clusters) {
NSDictionary *clusterDict = [self serializableDictionaryForCluster:cluster];
if (clusterDict == nil) {
if (!clusterDict) {
continue;
}
[response addObject:clusterDict];
Expand All @@ -91,7 +93,7 @@ - (void)clustersWithIdentifier:(NSString *)identifier result:(FlutterResult)resu

- (void)didTapOnCluster:(GMUStaticCluster *)cluster {
NSDictionary *clusterDict = [self serializableDictionaryForCluster:cluster];
if (clusterDict != nil) {
if (clusterDict) {
[self.methodChannel invokeMethod:@"cluster#onTap" arguments:clusterDict];
}
}
Expand All @@ -107,7 +109,7 @@ - (NSString *)clusterManagerIdentifierForCluster:(GMUStaticCluster *)cluster {
return nil;
}

if (cluster.items.firstObject && [cluster.items.firstObject isKindOfClass:[GMSMarker class]]) {
if ([cluster.items.firstObject isKindOfClass:[GMSMarker class]]) {
GMSMarker *firstMarker = (GMSMarker *)cluster.items.firstObject;
return [GoogleMarkerUtilities getClusterManagerIdentifierFrom:firstMarker];
}
Expand All @@ -121,7 +123,7 @@ - (NSString *)clusterManagerIdentifierForCluster:(GMUStaticCluster *)cluster {
/// @return NSDictionary if found otherwise nil.
- (NSDictionary *)serializableDictionaryForCluster:(GMUStaticCluster *)cluster {
NSString *clusterManagerId = [self clusterManagerIdentifierForCluster:cluster];
if (clusterManagerId == nil) {
if (!clusterManagerId) {
return nil;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ - (void)onMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if ([markerIdsToRemove isKindOfClass:[NSArray class]]) {
[self.markersController removeMarkersWithIdentifiers:markerIdsToRemove];
}
[self.clusterManagersController clusterAll];
[self.clusterManagersController invokeClusteringForEachClusterManager];
result(nil);
} else if ([call.method isEqualToString:@"markers#showInfoWindow"]) {
id markerId = call.arguments[@"markerId"];
Expand Down Expand Up @@ -336,7 +336,7 @@ - (void)onMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
result(nil);
} else if ([call.method isEqualToString:@"clusterManager#getClusters"]) {
id clusterManagerId = call.arguments[@"clusterManagerId"];
[self.clusterManagersController clustersWithIdentifier:clusterManagerId result:result];
[self.clusterManagersController serializeClustersWithIdentifier:clusterManagerId result:result];
} else if ([call.method isEqualToString:@"polygons#update"]) {
id polygonsToAdd = call.arguments[@"polygonsToAdd"];
if ([polygonsToAdd isKindOfClass:[NSArray class]]) {
Expand Down Expand Up @@ -575,7 +575,7 @@ - (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)
}

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
if ([marker.userData conformsToProtocol:@protocol(GMUCluster)]) {
if ([marker.userData isKindOfClass:[GMUStaticCluster class]]) {
GMUStaticCluster *cluster = marker.userData;
[self.clusterManagersController didTapOnCluster:cluster];
// When NO is returned, the map will focus on the cluster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ - (void)addMarker:(NSDictionary *)markerToAdd {
mapView:self.mapView];
[controller interpretMarkerOptions:markerToAdd registrar:self.registrar];
if (clusterManagerIdentifier && [clusterManagerIdentifier isKindOfClass:[NSString class]]) {
id clusterManager =
GMUClusterManager *clusterManager =
[_clusterManagersController clusterManagerWithIdentifier:clusterManagerIdentifier];
if (marker && [clusterManager isKindOfClass:[GMUClusterManager class]]) {
if (marker && clusterManager) {
[clusterManager addItem:(id<GMUClusterItem>)marker];
}
}
Expand Down Expand Up @@ -349,9 +349,9 @@ - (void)removeMarker:(NSString *)identifier {
}
id clusterManagerIdentifier = [controller clusterManagerIdentifier];
if ([clusterManagerIdentifier isKindOfClass:[NSString class]]) {
id clusterManager =
GMUClusterManager *clusterManager =
[_clusterManagersController clusterManagerWithIdentifier:clusterManagerIdentifier];
if (controller.marker && [clusterManager isKindOfClass:[GMUClusterManager class]]) {
if (controller.marker && clusterManager) {
[clusterManager removeItem:(id<GMUClusterItem>)controller.marker];
}
} else {
Expand Down

0 comments on commit 1669b76

Please sign in to comment.