efremidze / Cluster

Easy Map Annotation Clustering 📍
MIT License
1.28k stars 121 forks source link

how can i add multiple difference cluster? #134

Closed allenbbgo closed 3 years ago

allenbbgo commented 3 years ago

hi, i have problem.... i don't know how to create two difference cluster in one map

can you tell me how to add that? thank you

efremidze commented 3 years ago

I would suggest creating multiple ClusterManagers.

efremidze commented 3 years ago

If you are referring to one dataset but different UIs, you can customize the UI of the cluster view itself using the delegates.

allenbbgo commented 3 years ago

I would suggest creating multiple ClusterManagers.

thank you so much!!

ok , I have add two ClusterManagers,but i dont know how to make difference color at cluster

my problem is at viewFor function if i want to make diffenence cluster color, its change code in viewFor function ?

this my code

lazy var carManager: ClusterManager = { [unowned self] in
    let carManager = ClusterManager()
    carManager.delegate = self

    carManager.maxZoomLevel = 17
    carManager.minCountForClustering = 5
    carManager.clusterPosition = .nearCenter

    return carManager
}()

lazy var manager: ClusterManager = { [unowned self] in
    let manager = ClusterManager()
    manager.delegate = self

    manager.maxZoomLevel = 17
    manager.minCountForClustering = 1
    manager.clusterPosition = .nearCenter

    return manager
}()

my problem is in here if i want to make diffenence cluster color, its change code in here?

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation {
        print("MKUserLocation is nil")
        return nil
    }

    if let annotationCluster = annotation as? ClusterAnnotation {

        print("ClusterAnnotation")
        let index = 0
        let identifier = "Cluster\(index)"
        let selection = Selection(rawValue: index)!
        return mapView.annotationView(selection: selection, annotation: annotationCluster, reuseIdentifier: identifier)
    }
}
efremidze commented 3 years ago

If you subclass the annotations, you can differentiate between the annotations added to each manager. Use a different annotation subclass for each manager and check the type when you create the view.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if let cluster = annotation as? ClusterAnnotation {
        let child = cluster.annotations.first
        switch child {
        case is SomeAnnotationOne:
            return mapView.annotationView(selection: selection, annotation: child, reuseIdentifier: identifier)
        case is SomeAnnotationTwo:
            return mapView.annotationView(selection: selection, annotation: child, reuseIdentifier: identifier)
        default: break
        }
    }
}
allenbbgo commented 3 years ago

i did it!
,I don’t know how I could have done it without you I really appreciate it!