【问题标题】:How to handle multiple type of clustering in Google Map iOS SDK如何在 Google Map iOS SDK 中处理多种类型的集群
【发布时间】:2016-11-16 23:53:43
【问题描述】:

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

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

【问题讨论】:

  • 如果您可以分享显示问题的屏幕截图以及在 Google 地图上创建集群的代码,这将很有帮助。所以你的意思是说只有一个集群没有分解成单独的标记,而第二个是?这似乎是库中的错误,您可以继续向相关开发人员报告错误,以便他修复此问题。
  • 我想显示 2 种类型的集群,例如。一组集群为红色图标,另一组集群为蓝色图标,其中两种类型的集群代表 2 种不同类型的数据。
  • @PoojaSharma 我也需要这个确切的场景,你能解释一下你是如何创建两种类型的经理并处理的吗?

标签: ios google-maps


【解决方案1】:

这有点不明显,但是当您将集群项目添加到 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];

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

【讨论】:

    【解决方案2】:

    每个标记都有一个 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      相关资源
      最近更新 更多