简体   繁体   中英

How to make marker clustering with custom markers in GoogleMaps for iOS?

I'm a beginner in iOS development. I added a GoogleMaps on my app, I can cluster the default markers of GoogleMaps but when I custom markers by an image from my computer, the custom markers can't cluster. Someone can help me to do it in Swift 3?

I show you a part of my code :

override func viewDidLoad() {

mapView = GMSMapView(frame: view.frame)
mapView.camera = GMSCameraPosition.camera(withLatitude: 48.898902, longitude: 2.282664, zoom: 12.0)
mapView.mapType = .normal
mapView.delegate = self
view.addSubview(mapView)

if isClustering {
    var iconGenerator: GMUDefaultClusterIconGenerator!
    if isCustom { // Here's my image if the event are clustered
        var images: [UIImage] = [UIImage(named: "m1.png")!, UIImage(named: "m2.png")!, UIImage(named: "m3.png")!, UIImage(named: "m4.png")!, UIImage(named: "m5.png")!]
        iconGenerator = GMUDefaultClusterIconGenerator(buckets: [10, 20, 50, 100, 200], backgroundImages: images)
    } else {
        iconGenerator = GMUDefaultClusterIconGenerator()
    }

    let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
    let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator)

    clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer)
    clusterManager.cluster()
    clusterManager.setDelegate(self, mapDelegate: self)
} else {
}

// Here's my first custom markers (I don't write here others for simple code)
let firstLocation = CLLocationCoordinate2DMake(48.898902, 2.282664)
let marker = GMSMarker(position: firstLocation)
marker.icon = UIImage(named: "pointeurx1") //Apply custom marker
marker.map = mapView
}

func clusterManager() {...}

You can check the following topic : How to cluster custom icons markers in GoogleMaps for iOS How to clustered markers from Firebase in GoogleMaps for iOS

You could find a good answer because I had the same issue last week, just follow these advices. Hope it helps you a lot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM