Reputation: 353
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
Upvotes: 0
Views: 743
Reputation: 3157
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.
Upvotes: 0