0

I am implementing marker clustering in google map. I am getting data from api (a list of lat long and other information). using google-map-ios-utils library I am doing clustering but I am unable to attach data individually with each marker in the cluster. When I click the cluster it zooms in that is perfect but the delegate method didTapMarker is also triggered at cluster tap where as the delegate method didtapCluster is supposed to get triggered. And I have no idea how can I attach info to these markers when the get displayed after cluster click.

       override func viewDidLoad() {
        super.viewDidLoad()
     let iconGenerator = GMUDefaultClusterIconGenerator()
            let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
            let renderer = GMUDefaultClusterRenderer(mapView: self.mapView, clusterIconGenerator: iconGenerator)
            clusterManager = GMUClusterManager(map: self.mapView, algorithm: algorithm, renderer: renderer)
            renderer.delegate = self
            // clusterManager.cluster()
            clusterManager.setDelegate(self, mapDelegate: self)
     }

 func generatePOIItems(_ accessibilityLabel: String, position: CLLocationCoordinate2D, icon: UIImage?, markerValue: NSDictionary) {
        let item = POIItem(position: position, name: accessibilityLabel, markerData: markerValue)

        poitem.append(item)
        self.clusterManager.add(item)


    }
func addTocluster(){
        self.mapView.clear()
        clusterManager.clearItems()
        clsGlobal.initialService( completion: {(result)in

            print(result)
            self.arrdict = result as! NSDictionary
            let success = self.arrdict.value(forKey: "success") as! Int
            if success == 1 {

                self.locationcount = self.arrdict.value(forKey: "data") as! NSArray
                DispatchQueue.main.async {
                    for i in 0..<self.locationcount.count {
                        let locations = self.locationcount.object(at: i)as! NSDictionary
                        let lattitude = locations.value(forKey: "serviceLatitude") as! String
                        let longitude = locations.value(forKey: "serviceLongitude") as! String
                        let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: Double(lattitude) as! CLLocationDegrees, longitude: Double(longitude) as! CLLocationDegrees)
                        if locations.value(forKey: "companyName") is NSNull {}
                        else {
                            let name = locations.value(forKey: "companyName") as! String
                            // marker.title = "\(name)"
                        }
                        if locations.value(forKey: "iconUrl") is NSNull{}
                        self.generatePOIItems(String(format: "%d", i), position: location, icon: nil, markerValue: locations)

                    }
                    self.clusterManager.cluster()


                }
            }
        })
    }



class POIItem: NSObject, GMUClusterItem {
    var position: CLLocationCoordinate2D
    var name: String!
    var markData : NSDictionary!
    init(position: CLLocationCoordinate2D, name: String, markerData:NSDictionary) {
        self.position = position
        self.name = name
        self.markData = markerData
    }
}

here is some code. Xcode 10, swift 4

Wide Angle Technology
  • 1,184
  • 1
  • 8
  • 28
Rj19
  • 305
  • 1
  • 6
  • 20

1 Answers1

0

You need to use the delegate method of "GMUDefaultClusterRenderer" "GMUClusterRendererDelegate".

After setting renderer delegate to self, use

- (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker {
}

delegate method to get the corresponding marker and add info to it.

You can refer this link for more info.

Shreeram Bhat
  • 2,849
  • 2
  • 11
  • 19
  • link is empty, please check it. – Rj19 Sep 06 '19 at 05:21
  • Updated the link. You can check it now. – Shreeram Bhat Sep 06 '19 at 05:29
  • yes it is working. using renderer method I can now get marker event. – Rj19 Sep 06 '19 at 06:27
  • Can you accept the answer so that it might help others? – Shreeram Bhat Sep 06 '19 at 06:39
  • of course but what about the clusterManager:didTapCluster method, why is it not being called – Rj19 Sep 06 '19 at 07:06
  • I can't help much without seeing the code. By the way did you use -(void)setDelegate:(id _Nullable)delegate mapDelegate:(id _Nullable)mapDelegate; method to register for delegate? – Shreeram Bhat Sep 06 '19 at 07:14
  • The code looks good. Inside "GMUClusterManager" .m file you can find - (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {} method implementation under "GMSMapViewDelegate" section. You can put break point there to see exactly what is happening. – Shreeram Bhat Sep 06 '19 at 08:23
  • They are using - (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {} method to forward delegate calls there. – Shreeram Bhat Sep 06 '19 at 08:36