如何在Google Map iOS SDK

时间:2015-09-03 09:28:37

标签: ios google-maps

我可以通过创建2个GClusterManager对象来显示2种不同类型的聚类。但问题是群集项目被重置为新创建的GClusterManager对象,因此当我们放大地图时,第一个GClusterManager对象的项目也不会分解为单个标记。我在下面的类中使用聚类:

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

2 个答案:

答案 0 :(得分:0)

每个标记都有一个marker.userData值。

现在进入GDefaultClusterRenderer.m并查看此功能并在这里玩游戏:

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

答案 1 :(得分:0)

它有点不明显但是当您将集群项添加到GMUClusterManager时,事实上正在添加到GMUClusterAlgorithm。您需要确保正确地创建集群管理器。因此,当您从其中一个[clusterManager clearItems]清除项目时,其他经理群集仍然具有相关性。

// 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];

这两个集群生成器对我来说效果很好。虽然现在我的问题是这些集群相互重叠。

enter image description here