如何使用Google Cluster Manager自定义默认图钉标记?

时间:2019-04-04 17:23:45

标签: ios google-maps google-maps-markers markerclusterer

如何使用Google Cluster Manager自定义默认的NOT CLUSTER,而不是红色的大头针标记?周围也有类似的问题,但没有有效的答案。 当我使用时:

func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker) {

        marker.icon = UIImage(named: "my_marker")
        marker.setIconSize(scaledToSize: .init(width: 18, height: 24))

}

它也自定义群集和图标。如何自定义图钉标记而不是聚类标记?

2 个答案:

答案 0 :(得分:1)

marker具有属性iconView,它是UIView。由于它是一个UIView,因此您可以根据需要自定义。

func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker) {
    marker.iconView = MyView()
}

答案 1 :(得分:0)

在Swift 5中工作

enter image description here

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, willRenderMarker marker: GMSMarker) {

        if marker.userData is POIItem {

            if let item = marker.userData as? POIItem {
                marker.icon = item.icon
                marker.title = item.name
            }

        }

        if marker.userData is GMUStaticCluster {

            if let staticCluster = marker.userData as? GMUStaticCluster {

                //this is a custom UIView (.xib) contain a UIImage with custom marker and one label (countLabel) where show number of grouped markers
                let customClusterMarker:CustomClusterMarker = CustomClusterMarker()

                customClusterMarker.countLabel.text = "\(staticCluster.count)"

                let view = UIView(frame: CGRect(x: 0, y: 0, width: 52, height: 70))

                view.addSubview(customClusterMarker)
                marker.iconView = view

            }
        }


    }
}

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
     }
}