Search code examples
swiftmkmapviewrealm

Custom view for realm map clusters


As far as I read realm docs for map clustering there is a class ABFClusterAnnotationView which have few properties : count, color, countLabel but I can not find anywhere image for annotation. Is it the way to override this with adding an image property ? I manage to add images by using default annotation iside mapView delegate method but from there i can not manage of count of clusters. I would like to change the image only where there is only pin with 1 value on the map.

So there is nothing tricky, simple setting an image for an annotation :

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

        let annView = MKAnnotationView(annotation: annotation, reuseIdentifier: "test")
        annView.image = myImage

        return annView
    }

Thanks in advance!


Solution

  • IMHO image property is not overridden. But anyway you have still several options:

    1. Do not use ABFClusterAnnotationView. (use e.g. MKAnnotationView)
    2. Use FBAnnotationClusterView, set FBAnnotationClusterViewConfiguration and in template use FBAnnotationClusterDisplayMode.Image(imageName)
    3. Use different library - https://github.com/efremidze/Cluster

    Ad 1)

    override func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            switch annotation {
            case _ as MyAnnotation:
                // use MKAnnotationView
                let reuseId = "Pin"
                return mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView ?? MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            case let fbAnnotation as FBAnnotationCluster:
                let reuseId = "Cluster"
                let clusterView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) ?? FBAnnotationClusterView(annotation: fbAnnotation, reuseIdentifier: reuseId, configuration: FBAnnotationClusterViewConfiguration.default())
                return clusterView
            default:
                return nil
            }
    }