Arasuvel Arasuvel - 1 month ago 15
iOS Question

Clustering in Google Maps SDK for iOS

I have been using the google SDK in my iOS app. In which I used to marker for plotting the location.
I have so many markers in a particular, so I have decided to use the clustering concept here.

Please let me know how can use the clustering in google maps sdks.
Is there any library which can be used for clustering using google maps sdks.

Answer

Latest version of Google Maps SDK has option to cluster the marker. Please refer this link : https://developers.google.com/maps/documentation/ios-sdk/utility/marker-clustering

To use the cluster manager, instantiate a GMUClusterManager object and pass it a GMSMapView, a GMUClusterAlgorithm, and a GMUClusterRenderer. The library includes default implementations of the cluster algorithm and renderer. The following code creates a cluster manager using the GMUNonHierarchicalDistanceBasedAlgorithm and the GMUDefaultClusterRenderer that are included in the utility library:

- (void)viewDidLoad {
    [super viewDidLoad];

     // Set up the cluster manager with a supplied icon generator and renderer.
  id<GMUClusterAlgorithm> algorithm =
      [[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init];
  id<GMUClusterIconGenerator> iconGenerator =
      [[GMUDefaultClusterIconGenerator alloc] init];
  id<GMUClusterRenderer> renderer =
      [[GMUDefaultClusterRenderer alloc] initWithMapView:_mapView
                                    clusterIconGenerator:iconGenerator];
  _clusterManager =
      [[GMUClusterManager alloc] initWithMap:_mapView
                                   algorithm:algorithm
                                    renderer:renderer];

  // Generate and add random items to the cluster manager.
  [self generateClusterItems];

  // Call cluster() after items have been added
  // to perform the clustering and rendering on map.
  [_clusterManager cluster];
}

Feed your markers into the cluster as GMUClusterItem objects by calling clusterManager:addItem:. The following code randomly generates cluster items (POIs) within the scope of the map's camera, then feeds them to the cluster manager:

// Randomly generates cluster items within some extent of the camera and
// adds them to the cluster manager.
- (void)generateClusterItems {
  const double extent = 0.2;
  for (int index = 1; index <= kClusterItemCount; ++index) {
    double lat = kCameraLatitude + extent * [self randomScale];
    double lng = kCameraLongitude + extent * [self randomScale];
    NSString *name = [NSString stringWithFormat:@"Item %d", index];
    id<GMUClusterItem> item =
        [[POIItem alloc] initWithPosition:CLLocationCoordinate2DMake(lat, lng)
                                     name:name];
    [_clusterManager addItem:item];
  }
}

// Returns a random value between -1.0 and 1.0.
- (double)randomScale {
  return (double)arc4random() / UINT32_MAX * 2.0 - 1.0;
}

https://googlegeodevelopers.blogspot.in/2016/07/announcing-marker-clustering-in-google.html?_sm_au_=iVVkVV8Mt1WtfjFn