Pooja Sharma Pooja Sharma - 18 days ago 9
iOS Question

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

Answer

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;
    }
}
Comments