这是我用于实现集群的代码。从第一步开始。请让我知道以下代码是否有错误。
var clusterManager: GMUClusterManager?
var clusterItems : [POIItem] = []
func removeMarker(userId : Double){
for (index,item) in clusterItems.enumerated(){
if item.participantUserId == userId{
clusterItems.remove(at: index)
clusterManager?.remove(item)
}
}
clusterManager?.cluster()
}
这是该方法的委托方法
func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker) {
if let poiItem = marker.userData as? POIItem {
if participantImageView == nil{
participantImageView = RideParticipantsImageView.loadFromNibNamed(nibNamed: "RideParticipantImageView") as? RideParticipantsImageView
}
participantImageView!.participantImageView.image = poiItem.image
marker.icon = ViewCustomizationUtils.getImageFromView(view: participantImageView!)
for rideParticipantLocation in rideParticipantLocationObject{
if poiItem.participantUserId == rideParticipantLocation.userId!{
if isLocationUpdateExpired(ridePartcipantLocation: rideParticipantLocation){
marker.opacity = GoogleMapUtils.MARKER_HALF_OPACITY
}else{
marker.opacity = GoogleMapUtils.MARKER_FULL_OPACITY
}
}
}
marker.zIndex = 11
}
}
但是群集没有刷新,并且为群集图标拍摄了旧尺寸和新尺寸的图像。
class CustomizedCusterIconGenerator: GMUDefaultClusterIconGenerator{
var markerView: RideParticipantsCountView?
override func icon(forSize size: UInt) -> UIImage {
if size == 2{
let image = UIImage(named : "cluster_image1")
return image!
}
else if size == 3{
let image = UIImage(named : "cluster_image2")
return image!
}
else if size == 4{
let image = UIImage(named : "cluster_image3")
return image!
}
else if size == 5{
let image = UIImage(named : "cluster_image4")
return image!
}
else{
self.markerView = RideParticipantsCountView.loadFromNibNamed(nibNamed: "RideParticipantsCountView") as? RideParticipantsCountView
markerView?.setMarker(count: size)
UIGraphicsBeginImageContextWithOptions(markerView!.frame.size,false, 0)
markerView!.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
}
如果上面的代码中有任何错误。请帮忙。