Pooja Sharma
Pooja Sharma

Reputation: 43

How to handle multiple type of clustering in Google Map iOS SDK

I am able to show 2 different kind of clusters by creating 2 GClusterManager objects. But issue is cluster items are reset to newly created GClusterManager object, so when we zoom in map, items of first GClusterManager object are nor breaking into individual markers. I am using below classes for clustering:

https://github.com/DDRBoxman/google-maps-ios-utils

Upvotes: 3

Views: 2016

Answers (2)

user2557885
user2557885

Reputation: 69

It's a bit unobvious but when you add cluster items to GMUClusterManager item is adding to GMUClusterAlgorithm in fact. You need to be sure that you are creating cluster manager appropriately. So when you clear items from one of them [clusterManager clearItems] other manager clusters stay relevant.

// Common clusters setup

id<GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];

PinItemClusterRenderer *pin_renderer = [[PinItemClusterRenderer alloc] initWithMapView:self.mapView
                                                               clusterIconGenerator:iconGenerator];

//Pin clusters setup

id<GMUClusterAlgorithm> pin_algorithm =
[[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init];

[pin_renderer setMapView:self.mapView];
pin_cluster_manager =
[[GMUClusterManager alloc] initWithMap:self.mapView
                             algorithm:pin_algorithm
                              renderer:pin_renderer];


//Record clusterSetup

id<GMUClusterAlgorithm> record_algorithm =
[[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init];
RecordItemClusterRenderer *record_renderer = [[RecordItemClusterRenderer alloc] initWithMapView:self.mapView
                                                                           clusterIconGenerator:iconGenerator];
[record_renderer setMapView:self.mapView];
record_cluster_manager =  [[GMUClusterManager alloc] initWithMap:self.mapView
                                                       algorithm:record_algorithm
                                                        renderer:record_renderer];

this two cluster generators work work well for me. Though now I have a problem with those clusters overlap each other.

enter image description here

Upvotes: 1

Yaro
Yaro

Reputation: 1252

Every marker has a marker.userData value.

now Go into GDefaultClusterRenderer.m and look at this function and play around here:

- (void)clustersChanged:(NSSet*)clusters {
    for (GMSMarker *marker in _markerCache) {
        marker.map = nil;
    }

    [_markerCache removeAllObjects];

    for (id <GCluster> cluster in clusters) {
        GMSMarker *marker;
        marker = [[GMSMarker alloc] init];
        [_markerCache addObject:marker];

        marker.userData = cluster.marker.userData;

        NSUInteger count = cluster.items.count;
        if (count > 1) {
            marker.icon = [self generateClusterIconWithCount:count];
            NSMutableDictionary *newUserData = [NSMutableDictionary dictionaryWithDictionary:marker.userData];
            [newUserData setObject:[NSNumber numberWithBool:YES] forKey:@"isCluster"];
            marker.userData = [NSDictionary dictionaryWithDictionary:newUserData];
        }
        else {
            marker.icon = cluster.marker.icon;
            marker.groundAnchor = CGPointMake(0.5, 0.5);
            NSMutableDictionary *newUserData = [NSMutableDictionary dictionaryWithDictionary:marker.userData];
            [newUserData setObject:[NSNumber numberWithBool:NO] forKey:@"isCluster"];
            marker.userData = [NSDictionary dictionaryWithDictionary:newUserData];
        }

        marker.position = cluster.marker.position;
        marker.map = _map;
    }
}

Upvotes: 0

Related Questions