hulab / ClusterKit

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

GMSMarker to CKAnnotation #10

Closed MaeseppTarvo closed 7 years ago

MaeseppTarvo commented 7 years ago

Hey! I am trying to use this with Swift and GoogleMap. But the documentation is pretty small and it is hard for me to read Objective-c so I pretty much read comments. My problem is that how to add my GMSMarker as annotations into clusterManager since it is of type CKAnnotation?

I have Custom marker that inherits from GMSMArker.

This is what I am trying to do:

for location in self.pointOfInterests{

       let annotation = CustomMarker()
       annotation.markerID = location.id
       annotation.icon = #imageLiteral(resourceName: "MapPin")
       annotation.title = location.title
       annotation.snippet = location.description
       annotation.position = CLLocationCoordinate2D(latitude: location.lat, longitude: lng)
       annotation.map = self.mapView
       //Now here I should add those markers or annotations to the cluseterManager as self.mapView.addAnnotations(...)
}

In markerFor cluster: CKCluster I did like this:

func mapView(_ mapView: GMSMapView, markerFor cluster: CKCluster) -> GMSMarker {

        let marker = CustomMarker(position: cluster.coordinate)

        if cluster.count > 1 {
            marker.icon = UIImage(named: "cluster")
        } else {
            marker.icon = UIImage(named: "marker")
            marker.title = cluster.title
        }

        return marker;
    }

What should I do?

MaeseppTarvo commented 7 years ago

Whoever is struggling with this in the future:

  1. Create annotation that inherits from NSObject and CKAnnotation
class Annotation: NSObject, CKAnnotation{
    weak var cluster: CKCluster?

    var coordinate: CLLocationCoordinate2D
    var name: String!
    init(coordinate: CLLocationCoordinate2D, name: String){
        self.coordinate = coordinate
        self.name = name
    }
}
  1. Make array: var annotations:[Annotations] = []

  2. Append the annotations array and then assign this array to clusterManager self.mapView.clusterManager.annotations = self.annotations