ios - 如何处理谷歌地图中标记簇中的单个标记

标签 ios swift google-maps marker google-maps-ios-utils

我正在谷歌地图中实现标记聚类。我正在从 api 获取数据(经纬度和其他信息的列表)。使用 google-map-ios-utils 库我正在进行聚类,但无法将数据单独附加到聚类中的每个标记。当我单击集群时,它会放大,这是完美的,但是委托(delegate)方法 didTapMarker 也会在集群点击时触发,因为委托(delegate)方法 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”的委托(delegate)方法。

将渲染器委托(delegate)设置为 self 后,使用

- (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker {
}

委托(delegate)方法获取相应的标记并向其添加信息。

可以引用这个link了解更多信息。

关于ios - 如何处理谷歌地图中标记簇中的单个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57808038/

相关文章:

objective-c - 使用AVFoundation在iOS上录制音频样本

swift - 我想找到一种方法来定位确切的失败请求。(CFNetwork SSLHandshake 失败(-9806))

swift - sectionInset 无法使用 AutoLayout 在 iOS8 中使用 UICollectionView estimatedItemSize

java - 使用谷歌地图计算实时行进距离

javascript - 在谷歌地图中设置缩放

ios - 散点图对齐问题

ios - 要为 x 位置设置的约束

ios - 无法在函数内更新数组

php - 查找邮政编码 10 英里半径范围内的城镇。谷歌地图 API

ios - 如何正确跟踪 HealthKit 传统力量训练