使用群集在Google地图上自定义标记

时间:2017-05-09 09:00:55

标签: ios iphone google-maps swift3

我在我的应用程序中使用谷歌地图SDK并在选定的let long上绘制标记,并且在标记上制作组我正在使用GMUClusterManager在标记上添加群集,但是我正面临着更改标记图像的问题,如果我用GMUClusterManager绘制标记,则无法更改标记图像。因此,当用户放大地图或更改制作者的图像时,任何人都知道制作所有标记组的任何其他方法。

2 个答案:

答案 0 :(得分:10)

创建delete/update设置其委托后,我使用了我正在使用的视图控制器,然后实现了GMUClusterRendererDelegate

num.trees = 20

之后,您可以从协议中实现GMUDefaultClusterRenderer。通过此方法,您可以访问标记和标记中包含的数据。 使用If let语句访问数据并为标记提供您想要的iconView let iconGenerator = GMUDefaultClusterIconGenerator() let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm() let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator) renderer.delegate = self clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer)

答案 1 :(得分:0)

在Swift 5中工作

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, markerFor object: Any) -> GMSMarker? {
        switch object {
        case let item as POIItem:

            let marker = GMSMarker()

            marker.position = item.position
            marker.icon = UIImage(named: "your_custom_marker")

            return marker
        case let staticCluster as GMUStaticCluster:

            let marker = GMSMarker()

            marker.position = staticCluster.position
            marker.icon = UIImage(named: "your_gruped_custom_marker")

            return marker
        default:
            return nil
        }
    }
}

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