【问题标题】:How to handle individual markers in marker cluster in google map如何在谷歌地图中处理标记簇中的单个标记
【发布时间】:2019-09-05 14:49:28
【问题描述】:

我正在谷歌地图中实现标记聚类。我正在从 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

【问题讨论】:

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


    【解决方案1】:

    您需要使用“GMUDefaultClusterRenderer”“GMUClusterRendererDelegate”的委托方法。

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

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

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

    您可以参考此link 了解更多信息。

    【讨论】:

    • 链接为空,请检查。
    • 更新了链接。您现在可以查看。
    • 是的,它正在工作。使用渲染器方法我现在可以获得标记事件。
    • 你能接受这个答案以便帮助别人吗?
    • 当然可以,但是 clusterManager:didTapCluster 方法呢,为什么没有被调用
    猜你喜欢
    • 2011-11-08
    • 2019-06-10
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2013-10-16
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多