我正在Google地图中实现标记聚类。我从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,快速4
答案 0 :(得分:0)
您需要使用“ GMUDefaultClusterRenderer”,“ GMUClusterRendererDelegate”的委托方法。
将渲染器委托设置为self后,使用
- (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker {
}
委托方法以获取相应的标记并向其中添加信息。
您可以参考此link以获得更多信息。