如何处理 google 地图中标记簇中的单个标记
How to handle individual markers in marker cluster in google map
我正在 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
}
}
这是一些代码。
Xcode10,swift4
您需要使用"GMUDefaultClusterRenderer""GMUClusterRendererDelegate"的委托方法。
将渲染器委托设置为自身后,使用
- (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker {
}
委托方法获取相应的标记并向其添加信息。
您可以参考此 link 了解更多信息。
我正在 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
}
}
这是一些代码。 Xcode10,swift4
您需要使用"GMUDefaultClusterRenderer""GMUClusterRendererDelegate"的委托方法。
将渲染器委托设置为自身后,使用
- (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker {
}
委托方法获取相应的标记并向其添加信息。
您可以参考此 link 了解更多信息。