How to implement GMUClusterRenderer in Swift

后端 未结 6 554
忘了有多久
忘了有多久 2020-12-09 20:36

I am using Google Maps API for iOS and want to use marker clustering utility. I figured out how to show clustered markers, but I would like to customize markers. Can someone

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 21:03

    If you included Google-Maps-iOS-Utils source files to your project there are one "dirty" way to change icon of marker.

    Unfortunately there are no public methods to set custom icon, but you can change it in source file.

    In Google Map Utils/Clustering/View/GMUDefaultClusterRenderer.m

     - (void)renderCluster:(id)cluster animated:(BOOL)animated {
     ...
    
          GMSMarker *marker = [self markerWithPosition:item.position
                                                  from:fromPosition
                                              userData:item
                                           clusterIcon:[UIImage imageNamed:@"YOUR_CUSTOM_ICON"]
                                              animated:shouldAnimate];
     ...
    
    }
    

    Than you could setup your cluster manager (Swift)

     private func setupClusterManager() {
            let iconGenerator = GMUDefaultClusterIconGenerator()
            let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
            let renderer = GMUDefaultClusterRenderer(mapView: mapView,
                                                     clusterIconGenerator: iconGenerator)
    
    
            clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm,
                                               renderer: renderer)
    
    }
    

提交回复
热议问题