Reputation: 388
I am using google map SDK in my application and plotting marker on selected let long and for making groups on marker i am using GMUClusterManager for adding cluster on marker, but with this i am facing an issue to change the image of marker, with if i draw a marker with GMUClusterManager then there no option to change marker image. So any one know any other way to make groups of all markers when user zoom in map, or to change the image of maker.
Upvotes: 3
Views: 3319
Reputation: 892
Works in Swift 5
class POIItem: NSObject, GMUClusterItem {
var position: CLLocationCoordinate2D
var name: String!
var icon: UIImage
init(position: CLLocationCoordinate2D, name: String, icon: UIImage) {
self.position = position
self.name = name
self.icon = icon
}
}
class PrincipalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.google_map.clear()
let iconGenerator = GMUDefaultClusterIconGenerator()
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
let renderer = GMUDefaultClusterRenderer(mapView: self.google_map, clusterIconGenerator: iconGenerator)
renderer.delegate = self
self.clusterManager = GMUClusterManager(map: self.google_map, algorithm: algorithm, renderer: renderer)
self.clusterManager.setDelegate(self, mapDelegate: self)
//insert your markers type POIItem
//self.clusterManager.add(item)
self.clusterManager.cluster()
}
}
extension PrincipalViewController: GMUClusterRendererDelegate {
func renderer(_ renderer: GMUClusterRenderer, markerFor object: Any) -> GMSMarker? {
switch object {
case let item as POIItem:
let marker = GMSMarker()
marker.position = item.position
marker.icon = UIImage(named: "your_custom_marker")
return marker
case let staticCluster as GMUStaticCluster:
let marker = GMSMarker()
marker.position = staticCluster.position
marker.icon = UIImage(named: "your_gruped_custom_marker")
return marker
default:
return nil
}
}
}
extension PrincipalViewController: GMUClusterManagerDelegate {
func clusterManager(_ clusterManager: GMUClusterManager, didTap clusterItem: GMUClusterItem) -> Bool {
print("didTap clusterItem")
return true
}
func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) -> Bool {
print("didTap cluster")
return true
}
}
Upvotes: 2
Reputation: 1341
After creating your GMUDefaultClusterRenderer
set its delegate I used the view controller I was working in, and then implement the GMUClusterRendererDelegate
let iconGenerator = GMUDefaultClusterIconGenerator()
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator)
renderer.delegate = self
clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer)
After that you implement the func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker)
from the protocol. This method gives you access to the marker and the data enclosed in the marker.
Use an If let statement to access the data and give the marker the iconView you want if let markerData = marker.userData
Upvotes: 13