我正在为我的一个项目使用 Yandex Mapkit iOS SDK
看来,SDK允许添加地标是一个集群。我不能用userData添加自定义地标,就像将地标添加为mapObject一样。我想检测标记上的点击动作。

// adding markers as mapobjects:
let point = YMKPoint(coordinate: CLLocationCoordinate2D(latitude: Double(hit.geom!.lat ?? 0), longitude: Double(hit.geom?.lon ?? 0)))
let placemark: YMKPlacemarkMapObject
self.mapObjects = self.mapView.mapWindow.map.mapObjects

placemark = mapObjects!.addPlacemark(with: point, image: #imageLiteral(resourceName: "marker"))
placemark.userData = MarkerUserData(id: Int(hit.id!)!, description: hit.plate!)
placemark.isDraggable = false
placemark.addTapListener(with: self)

mapObjects!.addListener(with: self)
在集群中添加标记时,仅可以使用YMKPoint将标记添加到集群中。我找不到在集群内添加placemark对象的方法
let point = YMKPoint(coordinate: CLLocationCoordinate2D(latitude: Double(hit.geom!.lat ?? 0), longitude: Double(hit.geom?.lon ?? 0)))
let placemark: YMKPlacemarkMapObject

collection.addPlacemark(with: point, image: #imageLiteral(resourceName: "marker"))
// Placemarks won't be displayed until this method is called. It must be also called
// to force clusters update after collection change
collection.clusterPlacemarks(withClusterRadius: 20, minZoom: 5)

最佳答案

使用侦听器定义一个集合。用任何点填充数组。遍历数组并将每个点添加到集合中。向集合添加点时,将返回YMKPlacemarkMapObject,并添加用户数据。并扩展您的控制器委托方法。

并使用Yandex查看测试项目-https://github.com/yandex/mapkit-ios-demo/blob/master/MapKitDemo/ClusteringViewController.swift

class MapViewController: UIViewController {
    @IBOutlet weak var mapView: YMKMapView!
    var collection: YMKClusterizedPlacemarkCollection?
    var point: [YMKPoint] = [] // Fill the array with any points

    override func viewDidLoad() {
        super.viewDidLoad()

        collection = mapView.mapWindow.map.mapObjects.addClusterizedPlacemarkCollection(with: self)
        collection?.addTapListener(with: self)

        for point in points {
            let placemark = collection?.addPlacemark(with: point,
                                                           image: UIImage(named: "some_image")!,
                                                           style: YMKIconStyle.init())
            placemark?.userData = "user data"
        }

        collection.clusterPlacemarks(withClusterRadius: 60, minZoom: 15)
    }
}

extension MapViewController: YMKMapObjectTapListener {
    func onMapObjectTap(with mapObject: YMKMapObject, point: YMKPoint) -> Bool {
        guard let userPoint = mapObject as? YMKPlacemarkMapObject else {
            return true
        }

        print(userPoint.userData)
    }
}

10-08 05:25