在Android SDK中,有一个minClusterSize属性,当我们将其设置为1时,它会将每个元素显示为群集而不是标记。
但是在iOS SDK中,不存在minClusterSize和DefaultRenderer默认情况下显示低于3个项目,因为这些项目不是集群项目。
有什么解决办法吗?
谢谢。
最佳答案
import UIKit
class CustomClusterRenderer: GMUDefaultClusterRenderer {
let GMUAnimationDuration: Double = 0.5
var mapView: GMSMapView?
override init(mapView: GMSMapView, clusterIconGenerator iconGenerator: GMUClusterIconGenerator) {
super.init(mapView: mapView, clusterIconGenerator: iconGenerator)
self.mapView = mapView
}
func markerWith(position: CLLocationCoordinate2D, from: CLLocationCoordinate2D, userData: AnyObject, clusterIcon: UIImage, animated: Bool) -> GMSMarker {
let initialPosition = animated ? from : position
let marker = GMSMarker(position: initialPosition)
marker.userData = userData
marker.icon = clusterIcon
marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
marker.map = mapView
if animated {
CATransaction.begin()
CAAnimation.init().duration = GMUAnimationDuration
marker.layer.latitude = position.latitude
marker.layer.longitude = position.longitude
CATransaction.commit()
}
return marker
}
func getCustomUIImageItem(userData: AnyObject) -> UIImage {
if let item = userData as? Marker {
return item.merkerIcon
}
return UIImage()
}
override func shouldRender(as cluster: GMUCluster, atZoom zoom: Float) -> Bool {
print("Zoom Level is \(zoom) , and result is \(zoom<=14)")
return zoom <= 14;
}
}
class Marker: NSObject, GMUClusterItem {
var position: CLLocationCoordinate2D
var estate: Estate
init(estate: Estate) {
self.estate = estate
self.position = CLLocationCoordinate2D(latitude: estate.latitude,longitude: estate.longitude)
}
}