How to cluster custom icons markers in GoogleMaps for iOS
You are actually clustering first then adding markers thats why this is happening.
What you should actually do is
class MarkerModel: NSObject, GMUClusterItem {
var position: CLLocationCoordinate2D
var name: String
init(position: CLLocationCoordinate2D, name: String) {
self.position = position
self.name = name
}
}
override func viewDidLoad() {
super.viewDidLoad()
mapView = GMSMapView(frame: view.frame)
mapView.camera = GMSCameraPosition.camera(withLatitude: 13.756331, longitude: 100.501765, zoom: 12.0)
mapView.mapType = .normal
mapView.delegate = self
view.addSubview(mapView)
if isClustering {
var iconGenerator: GMUDefaultClusterIconGenerator!
if isCustom { // Here's my image if the event are clustered
var images: [UIImage] = [UIImage(named: "m1.png")!, UIImage(named: "m2.png")!, UIImage(named: "m3.png")!, UIImage(named: "m4.png")!, UIImage(named: "m5.png")!]
iconGenerator = GMUDefaultClusterIconGenerator(buckets: [5, 10, 15, 20, 25], backgroundImages: images)
} else {
iconGenerator = GMUDefaultClusterIconGenerator()
}
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator)
clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer)
} else {
}
}
func addMarkers(cameraLatitude : Float, cameraLongitude : Float) {
let extent = 0.01
for index in 1...clusterItemCount {
let lat = cameraLatitude + extent * randomScale()
let lng = cameraLongitude + extent * randomScale()
let name = "Item \(index)"
let position = CLLocationCoordinate2DMake(lat, lng)
let item = MarkerModel(position: position, name: name)
item.icon = #imageLiteral(resourceName: "marker")
clusterManager.add(item)
}
clusterManager.cluster()
clusterManager.setDelegate(self, mapDelegate: self)
}
func randomScale() -> Double {
return Double(arc4random()) / Double(UINT32_MAX) * 2.0 - 1.0
}
func renderer(_ renderer: GMUClusterRenderer, markerFor object: Any) -> GMSMarker? {
let marker = GMSMarker()
if let model = object as? MarkerModel {
// set image view for gmsmarker
}
return marker
}
func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) -> Bool {
let newCamera = GMSCameraPosition.camera(withTarget: cluster.position, zoom: mapView.camera.zoom + 1)
let update = GMSCameraUpdate.setCamera(newCamera)
mapView.moveCamera(update)
return false
}
How to make marker clustering with custom markers in GoogleMaps for iOS?
You can check the following topic :
How to cluster custom icons markers in GoogleMaps for iOS
How to clustered markers from Firebase in GoogleMaps for iOS
You could find a good answer because I had the same issue last week, just follow these advices.
Hope it helps you a lot
Customize the marker on google map with clusters
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
How to customize default pin marker using Google Cluster Manager?
marker
has a property iconView
which is a UIView. Since it's a UIView you can customize however you want.
func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker) {
marker.iconView = MyView()
}
how can I set custom marker icon with markers cluster in iOS Swift
1 : GO to Poitem class
Add one more element to that class
let marker = GMSMarker()
add that in init method too
and then simple called
let myitem = POitem()
myitem.marker = GMSMarker(latitude:0,longitude:0)
self.map.add(myitem)
google maps iOS SDK: custom icons to be used as markers
Here is what I have done
let marker = GMSMarker()
// I have taken a pin image which is a custom image
let markerImage = UIImage(named: "mapMarker")!.withRenderingMode(.alwaysTemplate)
//creating a marker view
let markerView = UIImageView(image: markerImage)
//changing the tint color of the image
markerView.tintColor = UIColor.red
marker.position = CLLocationCoordinate2D(latitude: 28.7041, longitude: 77.1025)
marker.iconView = markerView
marker.title = "New Delhi"
marker.snippet = "India"
marker.map = mapView
//comment this line if you don't wish to put a callout bubble
mapView.selectedMarker = marker
The output is
And my marker image was
You can change your color as per your need. Also if you want something in rectange, you can just create a simple small rectangular image and use it like I did above and change the color of your need.
Or if you want a rectangle with text within it, you can just create a small UIView
with some label and then convert that UIView
in UIImage
and can do the same thing.
//function to convert the given UIView into a UIImage
func imageWithView(view:UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
Hope it helps!!
How to clustered markers from Firebase in GoogleMaps for iOS
Firstly loadMarker()
should be called after the clusterManager is initialized i.e
clusterManager = GMUClusterManager(map: maMap, algorithm: algorithm, renderer: renderer)
then
clusterManager.cluster()
clusterManager.setDelegate(self, mapDelegate: self)
should be placed in loadMarker()
after for loop ends.
Your viewcontroller should conform to this protocol GMUClusterManagerDelegate
then add these 2 methods in viewcontroller.
func renderer(_ renderer: GMUClusterRenderer, markerFor object: Any) -> GMSMarker? {
let marker = GMSMarker()
if let model = object as? MarkerModel {
// set image view for gmsmarker
}
return marker
}
func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) -> Bool {
let newCamera = GMSCameraPosition.camera(withTarget: cluster.position, zoom: mapView.camera.zoom + 1)
let update = GMSCameraUpdate.setCamera(newCamera)
mapView.moveCamera(update)
return false
}
Try this and let me know if this works else we will try something else.
Related Topics
Swift Language Multicast Delegate
Routing Between Points with Mapbox
Xcode 6 Ignoring Breakpoints - Swift
How to Change the Number of Decimal Places iOS
Swift - How to Open Another Viewcontroller with Collectionviewcell Inside Uitableviewcell
Backgroundtimeremaining Returns (35791394 Mins)
How to Deal with Dynamic Sections and Rows in iOS Uitableview
Uitableviewcell Custom Class - Reload Cell Height After Change in Subview Height Constraint
Nsexceptionallowsinsecurehttploads Not Working for Ip Addresses
iOS Development App Startup Crash
How to Create an Alert in a Swift File Model That Works for Various View Controller
Get "No Keychain Available" Error When Try to Access Keychain from App Extension
Non-Main Bundle File as Alert Sound