google-maps-ios-utils icon indicating copy to clipboard operation
google-maps-ios-utils copied to clipboard

How to differenciate cluster from spot in mapView:didTapMarker: ?

Open MedericHenin opened this issue 10 years ago • 2 comments

I'm trying to do 2 different thing with this method. I'd like to zoom in if I tap on a cluster and display an alert if I tap on a spot, how can I do that?

MedericHenin avatar Nov 09 '15 10:11 MedericHenin

+1, I'm interested too.

chiliec avatar Nov 23 '15 06:11 chiliec

@CptMedo and @Chiliec : Subclass the GMSMarker and have two properties in it. Like below

@interface CustomGMSMarker : GMSMarker

@property (nonatomic) NSInteger count;
@property (nonatomic, strong) NSArray *markerArray;

@end

In GDefaultClusterRenderer class change the method - (void)clustersChanged:(NSSet*)clusters as below.

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

    [_markerCache removeAllObjects];

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

        NSUInteger count = cluster.items.count;
        marker.count = count;
        marker.markerArray = [cluster.items valueForKey:@"marker"];

        if (count > 1) {
            marker.icon = [self generateClusterIconWithCount:count];
        }
        else {
            marker.icon = cluster.marker.icon;
        }

        marker.userData = cluster.marker.userData;

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

And in your GMSMapViewDelegate method you will receive the custom marker, you can type cast and use it.

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker.

arasu01 avatar Jan 11 '16 12:01 arasu01