hulab / ClusterKit

An iOS map clustering framework targeting MapKit, Google Maps and Mapbox.
MIT License
512 stars 86 forks source link

(Impl question) access annotation view outside of mapView(_:viewFor:) #6

Closed rgdev closed 7 years ago

rgdev commented 7 years ago

Hello,

I keep an array of my annotations that I give to the clusterManager :

var myArray = [MyAnnotationThatExtendsCKAnnotation]() // class member
// I add a bunch of annotations in the array
map.clusterManager.annotations = myArray

At a later time I would like to access one of my annotation's corresponding annotation view (outside of mapView(_:viewFor:)) to update it (basically my annotations have a different visual for different states, state that I obtain after a network call)

I've tried something like this : let av = map.view(for: myArray[0]) as? MyAnnotationView

but I always end up with nil value.

Any idea why ? Would it be possible to update annotation views without removing/adding them back every time ?

Thanks.

rgdev commented 7 years ago

I found a solution. I wasn't able to get my annotations because those are wrapped in a CKCluster.

func onAnnotationStateReceived(_ identifier: Int) {
        let myAnnotation = myArray[identifier]
        for annotation in map.annotations {
            if let cluster = annotation as? CKCluster {
                if cluster.contains(myAnnotation) && cluster.count == 1 {
                    let av = mapview.view(for: cluster) as? MyAnnotationView
                    av?.updateState()
                }
            }
        }
    }