我正在谷歌地图中实现标记聚类。我正在从 api 获取数据(经度和其他信息的列表(。使用 google-map-ios-utils 库,我正在进行聚类分析,但我无法将数据单独附加到集群中的每个标记。当我单击集群时,它会放大,这是完美的,但委托方法 didTapMarker 也会在集群点击时触发,因为委托方法 didtapCluster 应该被触发。而且我不知道如何在群集单击后显示时将信息附加到这些标记。
override func viewDidLoad() {
super.viewDidLoad()
let iconGenerator = GMUDefaultClusterIconGenerator()
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
let renderer = GMUDefaultClusterRenderer(mapView: self.mapView, clusterIconGenerator: iconGenerator)
clusterManager = GMUClusterManager(map: self.mapView, algorithm: algorithm, renderer: renderer)
renderer.delegate = self
// clusterManager.cluster()
clusterManager.setDelegate(self, mapDelegate: self)
}
func generatePOIItems(_ accessibilityLabel: String, position: CLLocationCoordinate2D, icon: UIImage?, markerValue: NSDictionary) {
let item = POIItem(position: position, name: accessibilityLabel, markerData: markerValue)
poitem.append(item)
self.clusterManager.add(item)
}
func addTocluster(){
self.mapView.clear()
clusterManager.clearItems()
clsGlobal.initialService( completion: {(result)in
print(result)
self.arrdict = result as! NSDictionary
let success = self.arrdict.value(forKey: "success") as! Int
if success == 1 {
self.locationcount = self.arrdict.value(forKey: "data") as! NSArray
DispatchQueue.main.async {
for i in 0..<self.locationcount.count {
let locations = self.locationcount.object(at: i)as! NSDictionary
let lattitude = locations.value(forKey: "serviceLatitude") as! String
let longitude = locations.value(forKey: "serviceLongitude") as! String
let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: Double(lattitude) as! CLLocationDegrees, longitude: Double(longitude) as! CLLocationDegrees)
if locations.value(forKey: "companyName") is NSNull {}
else {
let name = locations.value(forKey: "companyName") as! String
// marker.title = "(name)"
}
if locations.value(forKey: "iconUrl") is NSNull{}
self.generatePOIItems(String(format: "%d", i), position: location, icon: nil, markerValue: locations)
}
self.clusterManager.cluster()
}
}
})
}
class POIItem: NSObject, GMUClusterItem {
var position: CLLocationCoordinate2D
var name: String!
var markData : NSDictionary!
init(position: CLLocationCoordinate2D, name: String, markerData:NSDictionary) {
self.position = position
self.name = name
self.markData = markerData
}
}
下面是一些代码。 Xcode 10, swift 4
你需要使用 "GMUDefaultClusterRenderer" "GMUClusterRendererDelegate" 的委托方法。
将渲染器委托设置为 self 后,使用
- (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker {
}
委托方法获取相应的标记并向其添加信息。
您可以参考此链接以获取更多信息。