I have a Google Map of GMSMapView Type and also I have markers shown on that map and I have also implemented Google clusters to that mapView.
So The thing I want to really achieve is that I want to tap on cluster icon and icon changes for a particular cluster. But currently that is not happening as when I tap on selected marker Only the individual marker does changes, but not the cluster Item does.
My CLuster Group Shown On Map
What I really want to achive
what I have actually achieved for the individual marker.
So the above image is of clustered markers I want to render an icon on that cluster item But unfortunately it’s not rendering as no image I am able to access in the delegates of GMUClusterManagerDelegate and GMUClusterRendererDelegate. I am not getting any way how can I make a cluster icon change on tap action of a cluster marker.
Note:- I want to change the icon on cluster tap for the cluster item And I have achieved for the selected individual marker
Below is the code that I have done so far
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
propertyMapView.delegate = self
createListOfPOIItems()
}
// MARK : -- Map related functions
//creating POOitems for the map
func createListOfPOIItems() {
self.propertyPOIItemList = []
for index in 0..<self.propertyListModalArray.count {
let propertylistmodal = self.propertyListModalArray[index]
if UtilitySharedClass.sharedInstance.isvalidGeoLat(propertylistmodal.propertylat) && UtilitySharedClass.sharedInstance.isvalidGeoLong(propertylistmodal.propertyLong) {
let coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(Float(propertylistmodal.propertylat!)!), longitude: CLLocationDegrees(Float(propertylistmodal.propertyLong!)!))
if universityLocation == coordinate{
continue
}
let item = POIItem(position: coordinate, name: propertylistmodal.propertyName ?? "", propertyListModal: propertylistmodal)
self.propertyPOIItemList?.append(item)
}
}
initiateClustering()
}
func initiateClustering() {
// Set up the cluster manager with the supplied icon generator and
// renderer.
let iconGenerator = ClusterIconGenerator()
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
let renderer = GMUDefaultClusterRenderer(mapView: propertyMapView,
clusterIconGenerator: iconGenerator)
clusterManager = GMUClusterManager(map: propertyMapView, algorithm: algorithm,
renderer: renderer)
renderer.delegate = self
renderer.minimumClusterSize = 2
//renderer.animationDuration = 0.2
// Generate and add random items to the cluster manager.
generateClusterItems()
// Call cluster() after items have been added to perform the clustering
// and rendering on map.
clusterManager.cluster()
clusterManager.setDelegate(self, mapDelegate: self)
self.setUniversityLocation()
}
adding cluster for map
/// adds property location to the cluster manager.
private func generateClusterItems() {
guard let items = self.propertyPOIItemList else { return }
items.forEach({ (item) in
clusterManager?.add(item)
clusterManager?.cluster()
})
}
Implementing the delegates for the cluster
extension SearchViewController: GMSMapViewDelegate, GMUClusterManagerDelegate, GMUClusterRendererDelegate {
func renderer(_ renderer: GMUClusterRenderer, markerFor object: Any) -> GMSMarker? {
let marker = CustomMarker()
if let itemMarker = object as? GMUCluster {
marker.markerType = .clusterType
marker.markerCount = itemMarker.items.count
} else if let itemMarker = object as? POIItem {
marker.markerType = .itemType
itemMarker.marker = marker
marker.propertyListModal = itemMarker.propertyListModal
}
return marker
}
func renderer(_ renderer: GMUClusterRenderer, didRenderMarker marker: GMSMarker) {
if let customMarker = marker as? CustomMarker {
if customMarker.markerType == .itemType {
let propertyMarker = createMarkerView(isSelected: customMarker.isSelected)
propertyMarker.markerPriceLabel.text = customMarker.propertyListModal?.propertyPrice
marker.icon = propertyMarker.asImage()
marker.groundAnchor = CGPoint(x: 0.5, y: 1)
}
}
}
// MARK: - GMUClusterManagerDelegate
func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) -> Bool {
let newCamera = GMSCameraPosition.camera(withTarget: cluster.position,
zoom: propertyMapView.camera.zoom)
let update = GMSCameraUpdate.setCamera(newCamera)
propertyMapView.moveCamera(update)
isClusterTapped = true
clusterPropertyList = []
let properties = cluster.items
properties.forEach({ (property) in
if let propertyModal = property as? POIItem, propertyModal.propertyListModal != nil{
clusterPropertyList.append(propertyModal.propertyListModal!)
}
})
if let previousMarker = propertyMapView.selectedMarker as? CustomMarker{
previousMarker.isSelected = !previousMarker.isSelected
let propertyMarker = createMarkerView(isSelected: previousMarker.isSelected)
propertyMarker.markerPriceLabel.text = previousMarker.propertyListModal?.propertyPrice
previousMarker.icon = propertyMarker.asImage()
}
self.propertyMapView.selectedMarker = nil
self.propertyListContainerForMap.isHidden = false
self.propertyViewForMapCollectionView.reloadData()
return true
}
func clusterManager(_ clusterManager: GMUClusterManager, didTap clusterItem: GMUClusterItem) -> Bool {
let newCamera = GMSCameraPosition.camera(withTarget: clusterItem.position,
zoom: propertyMapView.camera.zoom)
let update = GMSCameraUpdate.setCamera(newCamera)
propertyMapView.moveCamera(update)
if let previousMarker = propertyMapView.selectedMarker as? CustomMarker{
previousMarker.isSelected = !previousMarker.isSelected
let propertyMarker = createMarkerView(isSelected: previousMarker.isSelected)
propertyMarker.markerPriceLabel.text = previousMarker.propertyListModal?.propertyPrice
previousMarker.icon = propertyMarker.asImage()
}
let item = clusterItem as? POIItem
if let tappedMarker = item?.marker as? CustomMarker{
tappedMarker.isSelected = !tappedMarker.isSelected
let propertyMarker = createMarkerView(isSelected: tappedMarker.isSelected)
propertyMarker.markerPriceLabel.text = tappedMarker.propertyListModal?.propertyPrice
tappedMarker.icon = propertyMarker.asImage()
propertyMapView.selectedMarker = tappedMarker
}
isClusterTapped = false
self.propertyListContainerForMap.isHidden = false
self.propertyViewForMapCollectionView.reloadData()
return true
}
}
My POItem class
import Foundation
import GoogleMapsUtils
import GoogleMaps
class POIItem: NSObject, GMUClusterItem {
var position: CLLocationCoordinate2D
var name: String!
var propertyListModal: PeropertyListModel?
var marker: GMSMarker?
init(position: CLLocationCoordinate2D, name: String, propertyListModal: PeropertyListModel) {
self.position = position
self.name = name
self.propertyListModal = propertyListModal
}
}
class for IconGenerator
import Foundation
import GoogleMapsUtils
class ClusterIconGenerator: GMUDefaultClusterIconGenerator {
override func icon(forSize size: UInt) -> UIImage {
let image = textToImage(drawText: String(size) as NSString,
inImage: UIImage(named: "clusterMarker")!,
font: UIFont(name: "Futura-Medium", size: 12.0) ?? UIFont.systemFont(ofSize: 12))
return image
}
private func textToImage(drawText text: NSString, inImage image: UIImage, font: UIFont) -> UIImage {
UIGraphicsBeginImageContext(image.size)
image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
let textStyle = NSMutableParagraphStyle()
textStyle.alignment = NSTextAlignment.center
let textColor = UIColor.black
let attributes=[
NSAttributedString.Key.font: font,
NSAttributedString.Key.paragraphStyle: textStyle,
NSAttributedString.Key.foregroundColor: textColor]
// vertically center (depending on font)
let textH = font.lineHeight
let textY = (image.size.height-textH)/2
let textRect = CGRect(x: 0, y: textY, width: image.size.width, height: textH)
text.draw(in: textRect.integral, withAttributes: attributes)
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result!
}
}
The above is the code I did so far but not worth please can you find me the way out how can I achieve the solution of rendering an icon on cluster Item marker on tap action.


